How to Typedef a Template Class

How to typedef a template class?

Yes. It is called an "alias template," and it's a new feature in C++11.

template<typename T>
using MyVector = std::vector<T, MyCustomAllocator<T>>;

Usage would then be exactly as you expect:

MyVector<int> x; // same as: std::vector<int, MyCustomAllocator<int>>

GCC has supported it since 4.7, Clang has it since 3.0, and MSVC has it in 2013 SP4.

C++: Typedef of template class

namespace sc_dt {
template <int W> class sc_bv { ... } //meaning systemc_bit_vector
}

Has a non-type template parameter. When you instantiate an object of sc_bv you need to give it an int constant like

sc_dt::sc_bv<2> foo;

As you can see that is different than

typedef sc_dt::sc_bv<int> sc_vector;

Where you gave it a type, instead of a value.

If you know what value you want to use for sc_vector then you could use

typedef sc_dt::sc_bv<the_value_that_you_want_to_use> sc_vector;

or if you just want sc_vector to be a new name for the class template then you can use an alias template like

template<int value>
using sc_vector = sc_dt::sc_bv<value>;

which then lets you use sc_vector like

sc_vector<some_value> foo;

typedef template class instances

instrument_name::piano is not a type. Hence, you cannot use:

typedef instrument<instrument_name::piano> Piano;

You can use:

// Use a non-type template parameter.
template <instrument_name i_name>
class instrument : public instrument_base
{
public:
instrument():instrument_base(i_name)
{
cout << "I'm template class" << endl;
}
};

typedef instrument<instrument_name::piano> Piano;
Piano p;

How do typedefs work with templates?

When you typedef A B you are simply saying that B is another name for A.

Do all 3 typedefs contain area, height, and width variables?

Yes. The squareTemplate template class is defined to have area, height, and width and all instantiations of it will have those members. In the case of your typedefs:

typedef squareTemplate <std::vector<std ::pair<double, double>>, std::vector<std::pair<int, int>>> squareRange;

area has the type assumed by the first template parameter D, so std::vector<std ::pair<double, double>>; The same goes for height and width - they have the type of the second template argument, std::vector<std::pair<int, int>>

Following the same reasoning, you get:

typedef squareTemplate <bool, bool> squareValid;

all of them are bool

typedef squareTemplate<double, int> squareValue;

area is int; height and width are double

Howto use a c++ template typedef in a template class

I think this is what you want:

template <typename T>
class MySecondClass {
public:
MySecondClass(typename MyList<T>::Type& list) : list_(list) {}
private:
typename MyList<T>::Type& list_;
};

Typedef pointer in a Class Template

You should define TLLPtr as follows:

template<class T>
using TLLPtr = TLLNode<T>*;

And everywhere in the definition of TLL use TLLPtr<T> instead of just TLLPtr (and also change TLLNode to TLLNode<T>).

Update: I've also noticed that you have TLLNode<T> data; in TLLNode. You can't define a member of the class having the type of class itself, you probably meant T data;.

Is a typedef to self allowed over template parameters

No. The name of a template parameter is not allowed to be redeclared.

The name of a template parameter is not allowed to be redeclared
within its scope (including nested scopes). A template parameter is
not allowed to have the same name as the template name.

template<class T, int N>
class Y {
int T; // error: template parameter redeclared
void f()
{
char T; // error: template parameter redeclared
}
};

template<class X> class X; // error: template parameter redeclared

From the standard, [temp.local]/6:

The name of a template-parameter shall not be bound to any following
declaration contained by the scope to which the template-parameter
belongs. [Example 5:

template<class T, int i> class Y {
int T; // error: template-parameter hidden
void f() {
char T; // error: template-parameter hidden
}
friend void T(); // OK: no name bound
};

template<class X> class X; // error: hidden by template-parameter

— end example]

How to typedef template function pointer?

As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.

template <typename T>
void someVoidFunction() {}

using fPtrType = void(*)();

int main()
{
fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;
}

If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.

template <typename T, typename U>
T someFunction(U u) {}

template <typename T, typename U>
using fPtrType = T(*)(U);

int main()
{
fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;
}


Related Topics



Leave a reply



Submit