1

Seen it just now here.

Never met such construction before and I do not understand, what it means! And how it works in specialization, as typedefs do not generate new types:

Wrong:

template <typename T>
void a();
typedef int a_t;
typedef int b_t;
template<> void a<a_t>(){}
template<> void a<b_t>(){}

Compiles with warning: 'typedef' was ignored in this declaration, works as expected:

template <typename T>
void a();
typedef class a_t;
typedef class b_t;
template<> void a<a_t>(){}
template<> void a<b_t>(){}
Community
  • 1
  • 1
Lol4t0
  • 12,444
  • 4
  • 29
  • 65

3 Answers3

4

It means nothing, you better don't write it. Give a proper name to the typedef, if you want

typedef class a_t a_t;

I don't see any benefit of that though. A C programmer who recently changed over to C++ may be inclined to do this though, as in C you cannot refer to a class name ("tag", in C terms) by simply naming it, but you have to prefix it with the tag kind ("struct a_t", or "union a_t") or you make a typedef for it. However even in this setting, the programmer would have forgotten the name afterwards :)

In C++ that's not needed. In C++, the presence of both a typedef name and a class name is not resolved by keeping those names in different "namespaces" (so to speak, in C terms. Each "namespace" there is a kind of "syntax directed disambiguation". So that you can say "foo" and "struct foo" and "goto foo", and each time "foo" may be a totally different entity). In C++, the typedef name will replace the pure class name, and henceforth that name "a_t" is both - a typedef name and a class name.

As for the Standardese, we have an issue open for this, see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#157 .

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

This isn't an answer to the question in the title (Johannes explanation is clear on this point), but an explanation of the failure you saw.

The reason your first set of code fails is because a_t and b_t are the same type (int), and so you are creating two definitions of a<int>, which is what the compiler is complained about.

In your second example, you are declaring two different types (each is a name for a different class). So, you are creating two different functions, a<a_t> and a<b_t>.

jxh
  • 69,070
  • 8
  • 110
  • 193
1

I finally unsserstood, how it works.

typedef class a_t;

is not like

typedef int a_t;

but like

typedef int;

So, I forward declared class a_t and then typedef'd it to nothing. It also explains warning `typedef' was ignored in this declaration and all templates related stuff.

Lol4t0
  • 12,444
  • 4
  • 29
  • 65
  • If a piece of code you're asking about produces a compiler warning or other diagnostic, *please* include the message in your question. – Keith Thompson Aug 12 '12 at 21:07