Declarations that are impossible without typedef

P

Peter

I read somewhere that typedef keyword is not merely
a syntactic sugar, but there are, in fact,
valid declarations which would be impossible to express
without using typedef due to language syntax constraints.
If that's true, could you give me an example of
such declaration (the simpler, the better)?
 
Ö

Öö Tiib

I read somewhere that typedef keyword is not merely
a syntactic sugar, but there are, in fact,
valid declarations which would be impossible to express
without using typedef due to language syntax constraints.

Yes you can't do much template metaprogramming without
typedef. I think one should avoid template metaprogramming
regardless if it is clear how to use typedef there or not. ;)

Better stick to using typedef for convenient aliases for
long types (involving mix of arrays, templates and/or
functions).
If that's true, could you give me an example of
such declaration (the simpler, the better)?

Even simplest meta-manipulation of types without typedef
is impossible. For example how you would write things
like 'std::add_const' or 'std::remove_reference' without
it? These can look something like (I didn't test it):

template<typename T>
struct remove_pointer
{
typedef T type;
};

template<typename T>
struct remove_pointer<T*>
{
typedef T type;
};
 
Ö

Öö Tiib

Öö Tiib said:
how you would write things
like 'std::add_const' or 'std::remove_reference' without
[typdef]

using type = T;

Correct but that does not matter.

(7.1.3.2): A typedef-name can also be introduced by an alias-declaration.
The identifier following the using keyword becomes a typedef-name and
the optional attribute-specifier-seq following the identifier appertains
to that typedef-name. It has the same semantics as if it were introduced
by the typedef specifier.

So that particular form of 'using' is just a syntactic sugar defined in
terms of typedef.
 
M

Marc

Peter said:
I read somewhere that typedef keyword is not merely
a syntactic sugar, but there are, in fact,
valid declarations which would be impossible to express
without using typedef due to language syntax constraints.
If that's true, could you give me an example of
such declaration (the simpler, the better)?

You won't be able to test this one with most compilers, but try writing
a function with C++ linkage that takes as argument a pointer to a
function with C linkage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top