member function template error
-
hallo,
ich habe in etwa folgenden Sachverhalt:struct Foo { template<typename T> Foo() { } }; struct Bar: public Foo { Bar() : Foo<int>() // <<<<< { } }; int main(int argc, char* argv[]) { Bar bar; return 0; }Visual Studio kommt hier mit der kommentierten Zeile nicht klar, Meldung:
1> error C2059: syntax error : '<'
1> error C2512: 'Foo::Foo' : no appropriate default constructor availableMir scheint es so, als wuerde er irgendwie Foo als template betrachten wollen (und nicht kapieren, dass ich den Konstruktor meine). Uebergebe ich einen zusaetzlichen Parameter vom Typ T, also:
struct Foo { template<typename T> Foo(T t) { } }; struct Bar: public Foo { Bar() : Foo(1.0) { } };Frisst ers.
Was kann man da machen, ich brauche es ohneParameter? Foo selbst zum template zu machen steht ausser Frage.Viele Dank.
-
C++ Standard 14.8.17 schrieb:
[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. —end note ]
-
nimm doch nen dummy-parameter:
struct Foo { template<typename T> Foo(const T&) { } }; struct Bar: public Foo { Bar() : Foo(int()) {} }; int main() { Bar bar; }aber alles in allem klingt mir das ganze ein wenig fragwürdig...
vll schreibst du mal ein wenig mehr über den sinn dahinter?!bb
-
Wofür brauchst du einen Konstruktor mit Template-Parameter, wenn du den Parameter gar nicht benutzt? Das ist widersinnig!
-
Im Zweifel mit einem Typeholer als Dummy-Parameter:
template<typename T> struct Type {}; struct Foo { template<typename T> Foo(Type<T>) { } }; struct Bar: public Foo { Bar() : Foo(Type<int>()) {} }; int main() { Bar bar; }