简体   繁体   English

emplace_back和VC ++的挫败感

[英]emplace_back and VC++ frustration

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: 我正在使用Visual Studio 2012,尝试使用默认编译器和Nov CTP编译器,下面显示了我的问题:

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?? 为什么它看起来似乎emplace_back上限最多有5个参数? 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.. 他们甚至在http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx中说它需要一定数量的参数。

Is there any way around this, using VS2012? 有没有办法解决这个问题,使用VS2012?

It's a restriction caused by the previous Visual C++ compiler architecture. 这是由以前的Visual C ++编译器体系结构引起的限制。 Future versions of VC++ will lift that restriction and allow true variadic templates. VC ++的未来版本将解除限制并允许真正的可变参数模板。

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. 这将限制为6而不是5(最大可能值为10),但代价是编译速度降低。

The VS2012 November CTP compiler supports variadic templates, but their Standard Library was not yet updated in that release. VS2012 November CTP 编译器支持可变参数模板,但在该版本中尚未更新其标准库 Should be fixed in VS2013RC. 应该在VS2013RC中修复。 An upgrade is strongly recommended, because even the November CTP contained a lot of bugs. 强烈建议进行升级,因为即使是11月份的CTP也会出现很多错误。 If not possible, use the macro mentioned by Konrad Rudolph. 如果不可能,请使用Konrad Rudolph提到的宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM