Temlpate instantiation problem (part 2)

Started by
15 comments, last by Taldor 16 years, 5 months ago
Okay, forget that, I've looked at it.

//	This gives errors://	'invalid use of template-name ... without an argument list'template <class AA::A>	template <class BB::B>	Three<AA::A>::Three(const Three<BB::B> & other);


First, typenames are generally much easier to read without spaces in them.
Second, If you're trying to specialize this constructor, it would look more like

template <> Three<AA::A>::Three(const Three<BB::B>&);
Advertisement
Quote:Original post by Deyja
Second, If you're trying to specialize this constructor, it would look more like


It seems that he's not trying to specialize it, just to instantiate it.
Quote:Original post by ToohrVyk
Quote:Original post by Taldor
Sadly, this only works in this example and not in my actual code.


Erm. You're much more familiar with your own code than I am, so could you please say what the exact problem is in adapting this example to your code? The way I see it, you merely have to define a function which calls the appropriate constructor (something which should be possible if you ever intended to call that constructor in the first place);

I was wrong, I don't think it works in the example either, as I'm (trying to) instantiate the copy constructor you use in the convert function, so wrapping it in a function won't help, I believe.

Quote:Original post by Taldor
I was wrong, I don't think it works in the example either, as I'm (trying to) instantiate the copy constructor you use in the convert function, so wrapping it in a function won't help, I believe.


Using [foo] in a function will always instantiate [foo] if the function itself is instantiated at a point where the definition of [foo] is visible (that's the way template instantiation works). And so, by instantiating convert, I indirectly instantiate the constructor used inside.

Quote:Original post by ToohrVyk
Using [foo] in a function will always instantiate [foo] if the function itself is instantiated at a point where the definition of [foo] is visible (that's the way template instantiation works). And so, by instantiating convert, I indirectly instantiate the constructor used inside.


template <class FROM, class TO> void convert (const FROM & from){	return (void) TO (from);}template void convert <Three <BB :: B <YY :: Y> >, Three <AA :: A <XX :: X> > > (const Three <BB :: B <YY :: Y> > & from);

Using this code, I get this error: "undefined reference to `Three<AA::A<XX::X> >::Three<BB::B<YY::Y> >(Three<BB::B<YY::Y> > const&)'". So apparently it isn't instantiated.
test.cpp
#include <iostream>// Definition without function bodies                                                                                                                                class One {};class Two : public One {};template <class T> class Three : public Two{public :  Three ();  template <class U> Three (const Three <U> &);};namespace AA{  class A {};}namespace BB{  template <class T> class B : public AA::A {};}class X {};// Calls                                                                                                                                                             int main(){  Three< BB::B<X> > bx;  Three< AA::A > a (bx);}


defs.cpp
#include <iostream>// Definitions                                                                                                                                                       class One {};class Two : public One {};template <class T> class Three : public Two{public :  Three () {}  template <class U> Three (const Three <U> &) { std::cout << "Bar\n"; }};namespace AA{  class A {};}namespace BB{  template <class T> class B : public AA::A {};}class X {};// Instanciation                                                                                                                                                     template Three< BB::B<X> >::Three();template<class C1, class C2>void convert(const C2& o){  return (void) C1(o);}template void convert< Three<AA::A>, Three< BB::B<X> > >(const Three< BB::B<X> > &);


Compiles fine for me and outputs "Bar" even though neither constructor is available in the file where they are called. Using g++ 3.4.0
Ok, that works. :-S Thanks a lot!

This topic is closed to new replies.

Advertisement