C++ Template Typedef

C++ template typedef

C++11 added alias declarations, which are generalization of typedef, allowing templates:

template <size_t N>
using Vector = Matrix<N, 1>;

The type Vector<3> is equivalent to Matrix<3, 1>.


In C++03, the closest approximation was:

template <size_t N>
struct Vector
{
typedef Matrix<N, 1> type;
};

Here, the type Vector<3>::type is equivalent to Matrix<3, 1>.

templated typedef?

You can use C++11 templated type aliasing using using e.g. like this

template <typename T>
using gc_vector = std::vector<T, gc_allocator<T>>;

Note: I know this is an old question but since it has quite many upvotes and as it turns up in search results I thought it deserved an updated answer.

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

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;

How can I resolve this matter (c++ template with typedef)

When you are using a typedef for pair, you don't have your Trep class defined. Hence the error : "'Trep' was not declared in this scope"

You can move your typedef inside the class specification:

template <class type>
class Trep {

public:
type key;
int priority, size;
Trep *left, right;

Trep(type _key) :
key(_key), priority(rand()), size(1), left(NULL), right(NULL) {}


typedef std::pair<Trep<type>, Trep<type> > TrepPair; //error!

TrepPair splited(Trep &root, type key);
Trep* insert(Trep &root, Trep &node);

};

Also, the spelling of priority in the constructor member initializer list is wrong. It is misspelled as priorty

You also need to #include <cstdlib> for rand()

I am able to compile the code here

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.

Typedef with template parameter in C++

First of all, use the typename keyword to tell the compiler to interpret F as the (qualified) name of a type:

typedef std::vector<typename T::F> TFV;
// ^^^^^^^^

Secondly, TFV is not a type defined in the global namespace, so you have to properly qualify that as well in the definition of Function1():

    template<typename T>
typename C1<T>::TFV C1<T>::Function1()
// ^^^^^^^^ ^^^^^^^ ^^^
{ }

Finally, the definition of member functions of a class template should be placed in a header file, unless you are providing explicit instantiations for all the template instantiations you would otherwise implicitly generate.

Failing to do so will most likely result in an unresolved references error from the linker.

Template typedef?

I don't know how to define the template class Foo so that it's specialized to Foo1, Foo4, Foo8, Foo16

Like this:

template <int N> struct Foo_impl {};
template <> struct Foo_impl<1 > {using type = Foo1 ;};
template <> struct Foo_impl<4 > {using type = Foo4 ;};
template <> struct Foo_impl<8 > {using type = Foo8 ;};
template <> struct Foo_impl<16> {using type = Foo16;};
template <int N> using Foo = typename Foo_impl<N>::type;

But the problem is that template argument deduction is not going to work with such an alias:

template <int N> void bar(Foo<N> *foo) {}

int main()
{
Foo<4> x;
bar(&x); // error: no matching function for call to 'bar'
// note: candidate template ignored: couldn't infer template argument 'N'
}

To make it work, you have to use something like template <typename T> void bar(T *foo) {}, with a static_assert (or some other trick) to restricts T to one of those 4 types.

You can do something like this:

template <typename T> void bar(T *foo)
{
constexpr int N =
std::is_same_v<T, Foo1 > ? 1 :
std::is_same_v<T, Foo4 > ? 4 :
std::is_same_v<T, Foo8 > ? 8 :
std::is_same_v<T, Foo16> ? 16 : throw "Invalid T.";
// ...
}

Here, throw "Invalid T." doesn't actually throw at runtime, but causes a compile-time error if T is not one of Foo#.

c++ typedef to fix template args

No way that is quite so straight-forward. The only things that can be templates in C++03 are classes and functions. The good thing about classes, is that they themselves can contain a typedef as a member.

template<class A>
struct D {
typedef C<A, int> type;
};

So now D<A>::type stands for C<A, int>. This is what is known in template meta-programming as a meta-function. And it's good as you can make it in C++03.

While C++11 introduced alias templates, those require the new alias syntax, with the using keyword. An attempt with typedef, like you have, isn't valid C++11 either.

C++ Template using typename and passing typedef to function

template <typename T>
int insert_ord (T V[], int ll, int const LF, T e);

The function-template above will not work for arguments of type:

(int[999][20], int, int, int[20])

The first parameter of your insert_ord function is a 2D array of plate, while the last is of type plate which is a 1D array. You should declare the function-template like:

template <typename T, int sizeOuter, int sizeInner>
int insert_ord (T (&v)[sizeInner][sizeOuter], int ll, int const LF, T (&e)[sizeOuter]);

See it Live On Coliru


However, a few nitpicks with your code. you don't need that void in int main(). And prefer constexpr over #define if possible. Most importantly, use std::array<T, N> or consider wrapping your data structure in a class or struct



Related Topics



Leave a reply



Submit