80

I'm playing around with templates. I'm not trying to reinvent the std::vector, I'm trying to get a grasp of templateting in C++.

Can I do the following?

template <typename T>
typedef struct{
  size_t x;
  T *ary;
}array;

What I'm trying to do is a basic templated version of:

typedef struct{
  size_t x;
  int *ary;
}iArray;

It looks like it's working if I use a class instead of struct, so is it not possible with typedef structs?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
monkeyking
  • 6,670
  • 24
  • 61
  • 81

8 Answers8

173

The problem is you can't template a typedef, also there is no need to typedef structs in C++.

The following will do what you need

template <typename T> 
struct array { 
  size_t x; 
  T *ary; 
}; 
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • 4
    +1 for explaining the difference between your code and @monkeyking's. – rcollyer Mar 15 '10 at 16:58
  • How do you create a constructor with an initializer list for the `struct` if you don't `typedef` it? Make a class instead? [One answer here](https://stackoverflow.com/a/64750247/13132315) basically creates aliases for each type `T`, which isn't ideal. – Breno Jun 02 '22 at 12:42
  • 1
    @Breno: Can you ask that in a new question. I haven't written C++ code in 15 or more years now, sorry. – Binary Worrier Jun 02 '22 at 13:28
19
template <typename T>
struct array {
  size_t x;
  T *ary;
};
Andrey
  • 59,039
  • 12
  • 119
  • 163
9

You don't need to do an explicit typedef for classes and structs. What do you need the typedef for? Further, the typedef after a template<...> is syntactically wrong. Simply use:

template <class T>
struct array {
  size_t x;
  T *ary;
} ;
dirkgently
  • 108,024
  • 16
  • 131
  • 187
5

You can template a struct as well as a class. However you can't template a typedef. So template<typename T> struct array {...}; works, but template<typename T> typedef struct {...} array; does not. Note that there is no need for the typedef trick in C++ (you can use structs without the struct modifier just fine in C++).

sepp2k
  • 363,768
  • 54
  • 674
  • 675
4

The Standard says (at 14/3. For the non-standard folks, the names following a class definition body (or the type in a declaration in general) are "declarators")

In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the dec-laration shall contain at most one declarator. When such a declaration is used to declare a class template, no declarator is permitted.

Do it like Andrey shows.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
3

The syntax is wrong. The typedef should be removed.

Amar
  • 13,202
  • 7
  • 53
  • 71
webgenius
  • 51
  • 1
3

From the other answers, the problem is that you're templating a typedef. The only "way" to do this is to use a templated class; ie, basic template metaprogramming.

template<class T> class vector_Typedefs {
    /*typedef*/ struct array { //The typedef isn't necessary
        size_t x; 
        T *ary; 
    }; 

    //Any other templated typedefs you need. Think of the templated class like something
    // between a function and namespace.
}

//An advantage is:
template<> class vector_Typedefs<bool>
{
    struct array {
        //Special behavior for the binary array
    }
}
Narfanator
  • 5,595
  • 3
  • 39
  • 71
2

Looks like @monkeyking is trying it to make it more obvious code as shown below

template <typename T> 
struct Array { 
  size_t x; 
  T *ary; 
};

typedef Array<int> iArray;
typedef Array<float> fArray;