using `extern` with typedef-name - c++11

if we have
typedef std::vector<int> INT_VEC
if I use INT_VEC in any source file like this -
extern template class INT_VEC
I get error -
using typedef-name INT_VEC after class
Even using (aka through alias) does not solve it. How can it be resolved.

Something like this ?
extern INT_VEC your_external_variable;

Related

How to use `alias` attribute if symbol is defined in another file?

I am referring https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html to understand usage of alias attribute. It says, "It is an error if __f is not defined in the same translation unit."
I am using following code block in startup.c file in which __interrupt_handler is defined in another file (e.g. xyz.c).
startup.c:
void Interrupt102_Handler (void) __attribute__ ((weak, alias("__interrupt_handler")));
xyz.c:
void __attribute__ ((interrupt)) __interrupt_handler()
{
uint32_t interrupt_id;
}
What modification should I do to make it compile without any error?

Template template in c++11 error

I've been reading the chapter 5 of "C++ Templates, The complete guide", and i've seen the concept of "template template" templates, so tried myself.
In a template class, i've declared my template like this:
template <typename TipoClave, typename TipoDato,
template <class Elem, class Alloc = allocator<Elem>> class Lista = vector>
And this works. My problem comes when I try to use a different container than the default.
My class is as follows:
class Tabla
{
public:
struct Celda {
TipoClave clave;
TipoDato dato;
};
Tabla(unsigned tam)
{
t.resize(tam);
}
///< Some other functions
private:
typedef Lista<Celda> ListaDatos;
Lista<ListaDatos> t;
};
And then, when I try to use it from the main program like this:
int main (void)
{
Tabla<string,Alumno,array> tabla(200);
///< Some stuff
return 0;
}
But this line Tabla<string,Alumno,array> tabla(200); does not compile, getting me an error like:
test_tabla_t.cpp: In function ‘int main()’: test_tabla_t.cpp:20:27:
error: type/value mismatch at argument 3 in template parameter list
for ‘template class Lista> class Tabla’ Tabla
tabla(200);
I've tried to use Tabla<string,Alumno,vector> tabla(200);, and it works, so I don't know how to solve this error.
Assuming you're trying to use std::array, it does not take an Allocator template parameter. Its second argument is the size of the array.
template<
class T,
std::size_t N
> struct array;
You haven't mentioned how experienced you are with C++, but since you're trying to learn more, I'll say that I've been using C++ for nearly a decade and can count on one hand the number of times I've used template template parameters.
In this case, you want Lista to be "something that acts like a sequence container[1]", you might like to read about concepts[2], which may make it into C++20. They allow you to tell the compiler the interface you want a template parameter to have.
In the meantime, it might be easier to declare Lista as a plain template parameter.
[1] http://en.cppreference.com/w/cpp/concept/SequenceContainer
[2] https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)

Force Boost TypeIndex to report a specific pretty_name

I want to specify that a certain type reported by Boost TypeIndex boost::typeindex::type_id<T>().pretty_name() would yield a specific name.
The problem I want to solve is that, as reported in other places, there is a particular case that is confusing, that is the std::string type (or alias) gets reported as std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >.
(I understand the reason for this, std::string is an alias. I just want to override the reported string in this particular case.)
I followed the instructions here http://www.boost.org/doc/libs/1_59_0/doc/html/boost_typeindex/making_a_custom_type_index.html and the beginning of the custom code looks like this:
namespace my_namespace { namespace detail {
template <class T> struct typenum;
template <> struct typenum<void>{ enum {value = 0}; };
template <> struct typenum<std::string>{ enum {value = 1}; };
struct my_typeinfo {const char* const type_;};
const my_typeinfo infos[2] = {
{"void"}, {"std::string"}
};
...
...
But at the end, the most I can do is to replace one type reporting by another, instead of just amending one case.
One light weight solution could be to specialize boost::typeindex::stl_type_index (the output type of type_id<std::string>()) but by then the actual static information of the class is lost. (there is no class to specialize.)
But this cannot be done without a full specialization of typeid<std::string> which seems to difficult to do.
Is there a workaround for this? I would prefer a solution within Boost.Typeindex rather than runtime string replacement.
This is a very dirty way I found but it is not perfect and can create other problems down the road.
struct std_string{}; // dummy replacement class name
namespace boost{
namespace typeindex{
template<> boost::typeindex::stl_type_index type_id<std::string>() noexcept{
return type_id<std_string>(); // will report "std_string", ugly, but better than `std::__cxx11::basic_string<...>`
}
}}

How to define common enum in C++11?

So I've got some enum that is defined in one part and I need to use it in multiple other parts of program. As I suppose, there is no extern enums in C++11. So how to use the same defined enum in different units? Sorry for a duplicate or misunderstandings.
This seems to be exactly what header files are for:
enum_def.H:
enum class my_enum_type { /* .... */ };
file1.C:
#include <enum_def.H>
file2.C:
#include <enum_def.H>

Using a variable in different forms

I have a variable called AUTO in Form1 and i want to use it in a form called RoomStack.
I declared this in RoomStack.h:
static Form1 ^FM = gcnew Form1();
(so afterwards i'll write something like FM->AUTO)
But the declaration is giving errors:
error C2143: syntax error : missing ';' before '^'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C3845: 'myUI::RoomStack::FM': only static data members can be initialized inside a ref class or value type
Note that i encountered this issue before and the same method worked.
I had a form called NewGame, it contained a variable that i wanted to use in Form1, so in Form1.h i declared:
static NewGame ^NG = gcnew NewGame();
and it compiled.
Why am i having errors now?
Form1.h should be included in RoomStack.h
i.e adding the following line in RoomStack.h solves the problem:
#include "Form1.h"

Resources