emplace_back and VC++ frustration

北慕城南 提交于 2020-01-03 10:20:14

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/19200183/emplace-back-and-vc-frustration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!