具有函数本地类型的std :: vector <t>

时间:2017-02-07 18:22:07

标签: c++

为什么会这样

#include <vector>

void f()
{
    struct S
    {
        int first, second, third;
    };

    std::vector<S> vs;
}

使用Visual C ++ 2015,但不能使用g ++ 4.8.4?

1 个答案:

答案 0 :(得分:8)

确保您至少使用-std=c++0x进行编译。

在C ++ 11中,修改了标准以允许本地类成为模板参数(对于lambda支持)。如果你的目标是预C ++ 11,那就不行了。

如果您正在编译MSVC,默认情况下它将启用C ++ 11,而clang和pre-gcc 6则不然。

另请参阅:What restrictions does ISO C++03 place on structs defined at function scope?