Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typename, typedef, and resolution

Reply
Thread Tools

typename, typedef, and resolution

 
 
Gina Yarmel
Guest
Posts: n/a
 
      08-13-2003
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! ]
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-13-2003
"Gina Yarmel" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      08-13-2003
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! ]
 
Reply With Quote
 
Ben Hutchings
Guest
Posts: n/a
 
      08-13-2003
In article <(E-Mail Removed) >,
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! ]
 
Reply With Quote
 
Sergei Emantayev
Guest
Posts: n/a
 
      08-13-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Gina Yarmel) wrote in message
news:<(E-Mail Removed). 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! ]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hardware resolution and optical resolution? JethroUK© Digital Photography 4 05-24-2008 03:20 PM
How do you change the Modelsim Cursor Resolution (not simulation resolution) Andrew FPGA VHDL 0 09-26-2005 04:05 AM
Scanning resolution, printing resolution, and downsampling hassy_user Digital Photography 11 10-27-2004 07:18 PM
Resolution resolution Simon Digital Photography 4 02-27-2004 01:53 PM
ISO Resolution Chart and Printing Resolution Jack Yeazel Digital Photography 0 08-11-2003 11:19 PM



Advertisments