Hi!
I heard about Variadic templates and wanted to try them out. I wrote this little test example were I want to forward the arguments for an arbitrary class with an unknown number of arguments to the constructor. Most of the code is pretty much copied from different pages and put into this example.
My problem is that it doesn't compile at all and I don't really understand what the problem is. It seems like the compiler can't handle the template for the function. I post the log below the code.
If someone could figure this out or put me on the right track, that would be awesome.
Thank you!
4:#include "stdafx.h"
5:#include <iostream>
6:
7:using namespace std;
8:
9template<class T, class... Args>
10:T * create(Args&&... args)
11:{
12: return new T(std::forward<Args>(args)...);
13:}
14:
15:class testClass
16:{
17:public:
18: testClass(int a, double b)
19: {
20: cout << "c1" << endl;
21: }
22:
23: testClass(int& a, double& b, int c)
24: {
25: cout << "c2" << endl;
26: }
27:
28: ~testClass() {}
29:};
30:
31:int _tmain(int argc, _TCHAR* argv[])
32:{
33: testClass *t1 = create <testClass>(12, 1.0);
34: testClass *t2 = create <testClass>(12, 1.0, 1234);
35:
36: delete t2;
37: delete t1;
38:
39: return 0;
40:}
Build error log:
1>------ Build started: Project: TestVariadicTemplates, Configuration: Debug Win32 ------
1> TestVariadicTemplates.cpp
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(9): error C2332: 'class' : missing tag name
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(9): error C2993: '' : illegal type for non-type template parameter '<unnamed-tag>'
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(9): error C2143: syntax error : missing ',' before '...'
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(10): error C2065: 'Args' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(10): error C2988: unrecognizable template declaration/definition
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(10): error C2059: syntax error : '...'
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(34): error C2065: 'testClass' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(34): error C2065: 't1' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(34): error C2065: 'create' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(35): error C2065: 'testClass' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(35): error C2065: 't2' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(35): error C2065: 'create' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(37): error C2065: 't2' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(37): error C2541: 'delete' : cannot delete objects that are not pointers
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(38): error C2065: 't1' : undeclared identifier
1>e:\users\username\documents\visual studio 2012\projects\testvariadictemplates\testvariadictemplates\testvariadictemplates.cpp(38): error C2541: 'delete' : cannot delete objects that are not pointers
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========