小编omi*_*ron的帖子

奇怪的未初始化的const成员行为

请考虑以下代码段:

struct Foo {

};

template<typename T>
struct Bar {
    const T foo;
};

int main() {
    Bar<Foo> test;
}
Run Code Online (Sandbox Code Playgroud)

我用g ++ - 4.9.2用[-std = c ++ 11 -O0 -g3 -pedantic -Wall -Wextra -Wconversion]编译它并得到它error: uninitialized const member in ‘struct Bar<Foo>’.这很明显.

但是尝试添加std :: string作为Foo成员和程序编译!

#include <string>
struct Foo {
    std::string test;
};
// (...)
Run Code Online (Sandbox Code Playgroud)

发生了什么?将测试类型替换为double会导致程序无法再次编译.什么字符串成员在课堂上变化?

使用此代码段链接到在线编译器.

从版本4.6开始,gcc似乎就像那样.

c++ gcc c++11

17
推荐指数
1
解决办法
2497
查看次数

在VS 2013中使用初始化列表初始化地图的地图

我正在尝试使用C++ 11初始化地图地图.我的编译器是VS 2013 Express.

unordered_map<EnumType, unordered_map<string, string>> substitutions = {
    {
        Record::BasementType,
        {
            { "0", "" },
            { "1", "Slab or pier" },
            { "2", "Crawl" }
        }
    },
    {
        Record::BuildingStyle,
        {
            { "0", "" },
            { "1", "Ranch" },
            { "2", "Raised ranch" }
        }
    },
    // ... and so on
};
Run Code Online (Sandbox Code Playgroud)

它是编译但我在ntdll.dll中获得断点.但是这段代码的简化版本:

unordered_map<EnumType, unordered_map<string, string>> substitutions = {
    {
        Record::BasementType,
        {
            { "0", "" },
            { "1", "Slab or pier" },
            { "2", "Crawl" }
        }
    },
    // …
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11 visual-studio-2013

10
推荐指数
1
解决办法
2458
查看次数

标签 统计

c++ ×2

c++11 ×2

gcc ×1

stl ×1

visual-studio-2013 ×1