template question

Started by
6 comments, last by duke 19 years, 11 months ago
What syntax is required to do this (or if it is possible)?


template<class T>
struct foo
{
   template<class G>
   foo()
   {
      G g;
   }

   T t;
};

then to instantiate it:

foo<int> f<float>;
Gcc 3.3.3 gives an error. What I am trying to do is call a constructor in a templated class that has a template constructor which requires explicit specification of the template constructor argument. So really, can this be done, and if so what is the syntax... and btw my example is just to illustrate, that is not exactly what I am trying to do. Actual error msg: main.cpp: In function `int main()': main.cpp:26: error: template-id `f' used as a declarator main.cpp:26: error: no matching function for call to `foo::foo()' main.cpp:11: error: candidates are: foo::foo(const foo&) [edited by - duke on May 18, 2004 12:52:50 PM] [edited by - duke on May 18, 2004 12:53:39 PM]
super genius
Advertisement
I don''t see anything wrong with your syntax. I am not 100% this is supported by the language, but I don''t see why it shouldn''t be.

Maybe put parentheses after f?

foo f();

Regards,
Jeff


[ CodeDread ]
Is it possible that you are only allowed one default constructor? i.e. the non-templated default constructor with zero args is being generated by the compiler and it gets confused upon seeing your variable declaration.

I think a simple solution to this would be to put in a dummy argument of the corresponding inner template type:

  template<typename G>  foo(G* ptr) {...ptr is not necessarily used here}and thenfoo<int> f<float>(NULL);


Just a guess...

Regards,
Jeff

[ CodeDread ]
This worked with gcc 3.1 on linux:

template<class T,class G>struct foo {  foo() {    G g;  }  T t;};foo<int,float> f;


Ricardo, Brazil.

[edited by - rdfodra on May 18, 2004 1:32:29 PM]
Ricardo, Brazil
quote:Original post by rdfodra
This worked with gcc 3.1 on linux:

template<class T,class G>struct foo {  foo() {    G g;  }  T t;};foo<int,float> f;


Ricardo, Brazil.

[edited by - rdfodra on May 18, 2004 1:32:29 PM]


Yes that method works, but I don't want the template parameter G to be part of the type of the foo class. So I cannot use it.

I ended up going with the "dummy" arg approach for now, gcc 3.3.3 is not fully standards compliant, so even if we assume my example is valid code, it is entirely possible that it still wouldnt work on 3.3.3. I'll just live with it for now and when mingw gets a stable 3.4 build, I'll try it on that.

also I tried providing a default empty constructor and adding an arg to my template construct (a non template arg) and that did not fix the problem either.

Jeff

[edited by - duke on May 18, 2004 1:49:38 PM]
super genius
Why don''t you want to specify the type as part of the class? Is it absolutely required that the type in your constructor be templated? Just wondering...
From the C++ Standard, ISO/IEC 14882:2003 (the latest version), Section 14.5.2 paragraph 5, page 259:

Note: because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates.


Basically, what you''re trying to do isn''t supportted by the C++ language.
If you really don''t want the second template parameter for the outer class, then you can do a workaround like this:
template<class T>struct foo{	foo() { } 	template<class G>	init()	{		G g;	} 	T t;};foo<int> f;f.init<float>();

This topic is closed to new replies.

Advertisement