C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Cannot invoke a templated ctor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Momchil Velikov
Guest





PostPosted: Sun Sep 21, 2003 10:12 am    Post subject: Cannot invoke a templated ctor Reply with quote



The following program produces error at the definition of ``b''. However it
compiles fine the second (supposedly equivalent) form of the templated
constructor. Why the two alternatives aren't equivalent ?

template<class Obj>
struct ptr_to_member
{
typedef void (Obj::* type) ();
};

struct A
{
A (void (*fn) ())
{
}

#if 1
template<class Obj>
A (typename ptr_to_member <Obj>::type fn)
{
}
#else
template<class Obj>
A (void (Obj::* fn) ())
{
}
#endif
};

void foo ()
{
}

struct bar
{
void baz ()
{
}
};

A a (foo);

A b (&bar::baz);

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Graeme Prentice
Guest





PostPosted: Wed Sep 24, 2003 10:27 pm    Post subject: Re: Cannot invoke a templated ctor Reply with quote



On 21 Sep 2003 06:12:58 -0400, [email]velco (AT) fadata (DOT) bg[/email] (Momchil Velikov) wrote:

Quote:
The following program produces error at the definition of ``b''. However it
compiles fine the second (supposedly equivalent) form of the templated
constructor. Why the two alternatives aren't equivalent ?

Because ptr_to_member <Obj> is a non-deduced context - a nested name
specifier is a non deduced context - 14.8.2.4 para 4. The compiler
won't deduce Obj to be bar because the standard doesn't allow it to.

It was discussed here recently
http://makeashorterlink.com/?I2B622EF5

Graeme


Quote:

template<class Obj
struct ptr_to_member
{
typedef void (Obj::* type) ();
};

struct A
{
A (void (*fn) ())
{
}

#if 1
template A (typename ptr_to_member {
}
#else
template A (void (Obj::* fn) ())
{
}
#endif
};

void foo ()
{
}

struct bar
{
void baz ()
{
}
};

A a (foo);

A b (&bar::baz);



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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.