473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

typename, typedef, and resolution

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
4 3905
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Alexander Malkis | last post by:
What's the semantical/syntactical difference between two keywords "class" and "typename" (apart from different spelling)? -- Best regards, Alex. PS. To email me, remove "loeschedies" from...
2
by: Levent | last post by:
Please consider the following Parent and Child template classes: template <class T> class Parent { public: typedef T type; typedef T& ref; /* pack of typedefs */ };
1
by: lutorm | last post by:
Hi all, I'm working on migrating my code from KCC to gcc, and I'm having some issues with "implicit typename" warnings from gcc. Essentially, what happens is described by this example: ...
3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
1
by: Joel Kullberg | last post by:
Hi! I have a question for u guys! I would like to use the c++ package ITK (www.itk.org) for internal handling och data and functions in a dataset3D class of mine! I also want to use a base...
6
by: PengYu.UT | last post by:
Hi, I'm wondering why typename can not be inherented from base class. An example is shown below. If I have to typedef value_type anyway, I think it would be easier to define the 5 types in...
1
by: Carlos Martinez Garcia | last post by:
Hi all: I have the template class: template<typename InfoTabla> class TablaBusqueda { typename InfoTabla::Tabla TipoTabla; typename InfoTabla::Registro TipoRegistro; typename...
8
by: xuatla | last post by:
Hi, When I compile the following test code I got a warning about implicit typename. This happens in the member functions. Do you know the detail reason and solution? Thanks. - X ----------
1
by: ma740988 | last post by:
I'm wading my way through Josuttis template text. I'm having a hard time understanding some things. So given: template <class T> class generic_traits { public: typedef T value_type; };...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.