By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

typename, typedef, and resolution

P: n/a
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);
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Gina Yarmel" <ta**********@yahoo.com> wrote...
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
Jul 19 '05 #2

P: n/a
Gina Yarmel wrote:
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
some_func<s1>( foo());
does the trick.
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'.

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #3

P: n/a
In article <e4**************************@posting.google.com >,
Gina Yarmel wrote:
The foolowing error is mysterious to me. Isn't a typedef just a
synonym?

<snip>

Yes, but function template argument deduction doesn't work where the
function parameter type is a dependent name.

See
<http://groups.google.com/groups?selm=slrnbisehj.1pc.do-not-spam-benh%40tin.bwsint.com>
for a fuller explanation.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #4

P: n/a
ta**********@yahoo.com (Gina Yarmel) wrote in message
news:<e4**************************@posting.google. com>...
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!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.