typename, typedef, and resolution

G

Gina Yarmel

The foolowing error is mysterious to me. Isn't a typedef just a
synonym?

template<typename T>
typename T::me some_func(typename T::me A) {
return A;
}

template<typename T>
typename T::me other_func(T A) {
return A;
}

class some_class {
public:
typedef some_class me;
};

int main() {
some_class some_obj;
//ERROR:
//some_func(some_obj);
other_func(some_obj);
}
 
V

Victor Bazarov

Gina Yarmel said:
The foolowing error is mysterious to me. Isn't a typedef just a
synonym?

template<typename T>
typename T::me some_func(typename T::me A) {
return A;
}

template<typename T>
typename T::me other_func(T A) {
return A;
}

class some_class {
public:
typedef some_class me;
};

int main() {
some_class some_obj;
//ERROR:
//some_func(some_obj);
other_func(some_obj);
}

The compiler cannot resolve a template argument based on
a dependent name, IIRC. It cannot figure out that T is
'some_class' out of T::me when instantiating 'some_func'.

Victor
 
U

Ulrich Eckhardt

Gina said:
The foolowing error is mysterious to me. Isn't a typedef just a
synonym?

Yes, but that has nothing to do with your problem, I think.
template<typename T>
typename T::me some_func(typename T::me A) {
return A;
}

It can't work because supplying the type as parameter doesn't disambiguate
the other type it is typedef'd in. Imagine this:

struct foo
{};
struct s1
{
typedef foo me;
};
struct s2
{
typedef foo me;
};

some_func( foo());

The compiler gets a foo and can't possibly determine which of s1/s2 it
should be using. Not sure, but maybe using
template<typename T>
typename T::me other_func(T A) {
return A;
}

Here, the compiler gets a T and looks inside T for a type/typedef called
'me'.
 
S

Sergei Emantayev

(e-mail address removed) (Gina Yarmel) wrote in message
The foolowing error is mysterious to me. Isn't a typedef just a
synonym?

template<typename T>
typename T::me some_func(typename T::me A) {
return A;
}

template<typename T>
typename T::me other_func(T A) {
return A;
}

class some_class {
public:
typedef some_class me;
};

int main() {
some_class some_obj;
//ERROR:
//some_func(some_obj);
other_func(some_obj);
}

My VC6 fails with reasonable error: could not deduce template argument
for 'T'.
Typedef doesnt introduce a new type. But what you could do if you were
a c++ compiler? You have a template function call and you need to
identify the template argument in order to instantiate the function.
You are said: T::me is some_class, but nobody could tell you who is
mister T himself.

For example, we may have:

class some_class {
public:
typedef some_class me;
};

class other_class {
public:
typedef some_class me;
};

....
some_class some_obj;
some_func(some_obj); // ???? both some_class and other_class have
"me"
// of "some_class" type

For this reason you should explicitly tell the compiler which function
instance you want to use:

some_func<some_class> (some_obj); // OK!
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top