Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ==========
Posted
Updated 22-Jun-13 4:17am
v2
Comments
CHill60 22-Jun-13 10:36am    
I'm guessing it's the class... Args bit. Should this not be typename...? You've got VS2012 so the compiler should be able to handle variadic templates (I think they were introduced to C++ with 2010).
WaZoX 22-Jun-13 10:51am    
I tried using typename instead but it was still the same problem, just that the first error was now "error C2143: syntax error : missing ',' before '...'" instead. From what I understand from the documentation it should have the same meaning though.

1 solution

According to http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx[^] visual studio still doesn't support Variadic templates. That's the problem. If you want those features you can download "Microsoft Visual C++ Compiler Nov 2012 CTP" from microsoft. It's however a CTP version so you probably don't wanna release your software compiled with it I guess. I hope those features are introduced in the coming update 3 this year. I consider the question solved now.
 
Share this answer
 
v2
Comments
CHill60 22-Jun-13 11:41am    
I thought they did some sort of "simulation" of VTs but haven't had an opportunity to dig into it yet. Might be worth trying an alternative compiler/IDE
WaZoX 22-Jun-13 11:59am    
I found "Microsoft Visual C++ Compiler Nov 2012 CTP" which will enable it in visual studio. I updated the answer with it also. Thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900