c++ - C++11 统一初始化语法

标签 c++ c++11 uniform-initialization

关于C++11中“统一初始化语法”的问题。

在 C++11 中使用下一个语法初始化结构是否合法(查看第 128-137 行)?还是 POD 仍然存在?

http://pastebin.com/GMZ5QDmR

MSVC 2013 编译器的问题。此示例编译成功,但因错误的函数调用异常而崩溃。这告诉我 std::function 对象未正确初始化。

顺便说一句,ICC 13.0 无法编译示例中的代码。

example.cpp(130): error #2084: designator may not specify a non-POD (Plain Old Data) subobject

它是编译器的缺陷吗?或者编译器一切正常,但这种方法不符合 C++11?

这是一个简短的例子:

#include <functional>
#include <memory>

struct dispatcher_t {};
struct binder_t {};

struct factories_t
{
    std::function< std::unique_ptr< dispatcher_t > () > m_disp_factory;
    std::function< std::unique_ptr< binder_t > () > m_bind_factory;
};

std::unique_ptr< dispatcher_t >
create_dispatcher()
{
    return std::unique_ptr< dispatcher_t >( new dispatcher_t() );
}

std::unique_ptr< binder_t >
create_binder()
{
    return std::unique_ptr< binder_t >( new binder_t() );
}

void main()
{
    factories_t f{
        []() { return create_dispatcher(); },
        []() { return create_binder(); }
    };
}

最佳答案

首先:void main在C++中是非法的。使用 int main

Microsoft Visual Studio 2013 并不是 C++11 类的明星。英特尔的编译器版本 13 也不是,因为它们有版本 14。也就是说,您可以尝试 MSVC++ 2013 November CTP ,这在语言支持方面稍微好一点。

如果您想查看您的代码是否有效,请使用最新版本的 GCC 或 Clang,两者均可在 the Greatest Online Compiler Platform in Existence 上获得.

供应商网站上报告了 C++ 语言特性:

对于 C++11,任何其他 C++ 编译器都不值得一提。

请注意,这些页面可能未提及标准库功能。

您正在查看的功能通常属于“(一般)初始化列表”。

关于c++ - C++11 统一初始化语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25012030/

相关文章:

c++ - 像 Adob​​e Photoshop 一样的 OpenCV 和 USM 锐化

c++ - VC++ : Pass multiple arrays between functions

c++ - 为什么在 C++14 中不推荐使用 std::shuffle 方法?

C++0x统一初始化 "oddity"

c++ - 为什么具有私有(private)成员的聚合不支持 C++ 大括号初始化?

c++ - 原子结构的统一初始化?

c++ - 使用多个框架给出 clang : error: linker command failed with exit code 1

c++ - C++ 中的翻译单元与编译单元与目标文件与可执行文件与....

C++ 原子对象无锁保证

c++ - 为什么 MSVC++11 拒绝函数的 constexpr 限定?