Template Confusion: Short Code I promise!

T

Tomás

I can't understand why this won't compile:

template <class T>
struct Monkey {

typedef T Normal;

};

template <class T>
int Func()
{
typedef Monkey<T>::Normal SpecialType;

SpecialType k = SpecialType();

return k;
}

int main()
{
Func<int>();
}

-Tomás
 
V

Victor Bazarov

Tomás said:
I can't understand why this won't compile:

template <class T>
struct Monkey {

typedef T Normal;

};

template <class T>
int Func()
{
typedef Monkey<T>::Normal SpecialType;

I think this is covered in the FAQ. See 'dependent names'.
SpecialType k = SpecialType();

return k;
}

int main()
{
Func<int>();
}

V
 
T

Tomás

Victor Bazarov posted:

I think this is covered in the FAQ. See 'dependent names'.


I'm a idiot!


Note to self: Next time I encounter a template error, repeat "typename" to
myself ten times.


Thanks Victor.


-Tomás
 
D

Diego Martins

I know sometimes "typename" and ".template " are necessary

but this sounds too bloated and cumbersome to me

why did C++ become so illegible?
 
L

Luke Meyers

Diego said:
I know sometimes "typename" and ".template " are necessary

but this sounds too bloated and cumbersome to me

why did C++ become so illegible?

I highly suggest reading Stroustrup's informative and entertaining _The
Design and Evolution of C++_ (amazon.com/gp/product/0201543303), to
understand how issues like this have quite defensibly arisen. There
tend to be pretty good reasons for things like this, annoying as they
can sometimes be.

Luke
 

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

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top