Click here to Skip to main content
15,891,981 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionCompiler Error in Template Functions...? [modified] Pin
Joseph Dempsey5-Nov-10 7:41
Joseph Dempsey5-Nov-10 7:41 
AnswerRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 7:52
professional«_Superman_»5-Nov-10 7:52 
GeneralRe: Compiler Error in Template Functions...? Pin
Joseph Dempsey5-Nov-10 8:01
Joseph Dempsey5-Nov-10 8:01 
GeneralRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 8:02
professional«_Superman_»5-Nov-10 8:02 
GeneralRe: Compiler Error in Template Functions...? Pin
Joseph Dempsey5-Nov-10 8:17
Joseph Dempsey5-Nov-10 8:17 
GeneralRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 8:56
professional«_Superman_»5-Nov-10 8:56 
GeneralRe: Compiler Error in Template Functions...? Pin
Aescleal5-Nov-10 9:12
Aescleal5-Nov-10 9:12 
AnswerRe: Compiler Error in Template Functions...? [modified] Pin
Aescleal5-Nov-10 9:08
Aescleal5-Nov-10 9:08 
Have a dig in the standard for the rules on overload resolution and how it interacts with template expansion. As both VC++ later than 2002 and gcc 4.x agree on the behaviour it's probably standard. Essentially it looks like what's happening is that clone<T> is not part of the overload set for Y::clone() and X::clone() as there functions with that name already in the class of the pointer you're calling through.

One of three solutions spring to mind. Either make clone<t> an ordinary function, change it's name or call it explicitly. e.g. if you change your base class to:

class Cloneable
{
	public:
		template<class TYPE> TYPE* clone_as() const { return dynamic_cast< TYPE* > ( clone() ); }
		virtual Cloneable* clone() const = 0;
};


and change your client code to match things work the way I think you think they should. Incidentally be very careful writing code like this... You can write leaky code very easily:

int main()
{
    X x( 7 );
    Cloneable *p = &x;

    Y *py = p->clone_as<Y>();
}


leaks an X. I'd be tempted to return shared_ptrs or auto_ptrs to avoid this sort of faux pas.

Basically the interaction between overloading, overriding and templates (i.e. when templates are expanded and considered part of the set of overload candidates) is a nightmare. My advice would be avoid it unless you're writing a compiler.

Cheers,

Ash

PS: This is very similar to the well known interaction between overrides and overloads with implicit conversion:

class base
{
    public:
		virtual void do_something( int ) { std::cout << "Doing the base class int thing" << std::endl; }
		virtual void do_something( double ) { std::cout << "Doing the base class double thing" << std::endl; }
};

class derived : public base
{
    public:
		virtual void do_something( double )  { std::cout << "Doing the derived class double thing" << std::endl; }
};

int main()
{
	derived d;
	d.do_something( int( 7 ) );
}


This could always prints the derived class double message. You get around this by either changing the name or calling the function explicitly.

PPS: Superman's solution from the mouth of Bjarne would work as well - I didn't consider it as using declarations have their own fun properties which you may or may not be happy with.

modified on Saturday, November 6, 2010 2:48 AM

AnswerRe: Compiler Error in Template Functions...? [modified] Pin
Paul Michalik6-Nov-10 1:42
Paul Michalik6-Nov-10 1:42 
QuestionUsing a Message-Only Window to Receive Device Change Notification Messages [modified] Pin
Jim Fell5-Nov-10 5:11
Jim Fell5-Nov-10 5:11 
AnswerRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn5-Nov-10 5:32
sitebuilderLuc Pattyn5-Nov-10 5:32 
GeneralRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Jim Fell5-Nov-10 5:41
Jim Fell5-Nov-10 5:41 
GeneralRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn5-Nov-10 5:49
sitebuilderLuc Pattyn5-Nov-10 5:49 
AnswerRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn8-Nov-10 12:17
sitebuilderLuc Pattyn8-Nov-10 12:17 
QuestionWhy IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 2:17
professionalChesnokov Yuriy5-Nov-10 2:17 
QuestionRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
CPallini5-Nov-10 3:32
mveCPallini5-Nov-10 3:32 
AnswerRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 3:55
professionalChesnokov Yuriy5-Nov-10 3:55 
QuestionRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
CPallini5-Nov-10 4:13
mveCPallini5-Nov-10 4:13 
AnswerRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 5:50
professionalChesnokov Yuriy5-Nov-10 5:50 
QuestionProverbial good example of CTreeCtrl Pin
federico.strati4-Nov-10 23:56
federico.strati4-Nov-10 23:56 
AnswerRe: Proverbial good example of CTreeCtrl Pin
Sauro Viti5-Nov-10 1:41
professionalSauro Viti5-Nov-10 1:41 
GeneralRe: Proverbial good example of CTreeCtrl Pin
federico.strati5-Nov-10 2:51
federico.strati5-Nov-10 2:51 
GeneralRe: Proverbial good example of CTreeCtrl Pin
CPallini5-Nov-10 3:04
mveCPallini5-Nov-10 3:04 
GeneralRe: Proverbial good example of CTreeCtrl PinPopular
David Crow5-Nov-10 3:09
David Crow5-Nov-10 3:09 
GeneralRe: Proverbial good example of CTreeCtrl Pin
federico.strati5-Nov-10 3:38
federico.strati5-Nov-10 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.