I'm using Visual Studio 2012, trying this both with the default compiler and the Nov CTP compiler, and the following below shows my problem:
struct doesCompile
{
int mA, mB, mC, mD, mE;
doesCompile(int a, int b, int c, int d, int e) : mA(a), mB(b), mC(c), mD(d), mE(e)
{
}
};
struct doesNotCompile
{
int mA, mB, mC, mD, mE, mF;
doesNotCompile(int a, int b, int c, int d, int e, int f) : mA(a), mB(b), mC(c), mD(d), mE(e), mF(f)
{
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<doesCompile> goodVec;
goodVec.emplace_back(1, 2, 3, 4, 5);
std::vector<doesNotCompile> badVec;
badVec.emplace_back(1, 2, 3, 4, 5, 6); // error C2660: 'std::vector<_Ty>::emplace_back' : function does not take 6 arguments
return 0;
}
Why on earth does it seem emplace_back is capped at a maximum of 5 arguments?? They even say in http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx that it would take an arbitary amount of arguments..
Is there any way around this, using VS2012?
This function is used to insert a new element into the vector container, the new element is added to the end of the vector. Syntax : vectorname. emplace_back(value) Parameters : The element to be inserted into the vector is passed as the parameter.
With the simple benchmark here, we notice that emplace_back is 7.62% faster than push_back when we insert 1,000,000 object (MyClass) into an vector.
because emplace_back would construct the object immediately in the vector, while push_back , would first construct an anonymous object and then would copy it to the vector. For more see this question. Google also gives this and this questions.
It’s a restriction caused by the previous Visual C++ compiler architecture. Future versions of VC++ will lift that restriction and allow true variadic templates.
For the moment, you can statically raise the maximum limit of faux variadic templates by adding the following before any include into your code:
#define _VARIADIC_MAX 6
This will set the limit to 6 instead of 5 (up to a maximum possible value of 10) at the cost of decreased compilation speed.
The VS2012 November CTP compiler supports variadic templates, but their Standard Library was not yet updated in that release. Should be fixed in VS2013RC. An upgrade is strongly recommended, because even the November CTP contained a lot of bugs. If not possible, use the macro mentioned by Konrad Rudolph.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With