1

关于 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 specified an non-POD (Plain Old Data) 子对象

它是编译器的缺陷吗?或者编译器一切正常,但这种方法不符合 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(); }
    };
}
4

1 回答 1

2

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

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

如果您想查看您的代码是否有效,请使用最新版本的 GCC 或 Clang,它们都可以在Greatest Online Compiler Platform in Existence上找到。

供应商网站上报告了 C++ 语言功能:

任何其他 C++ 编译器都不值得在 C++11 中提及。

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

您正在寻找的功能通常属于“(通用)初始化列表”。

于 2014-07-29T14:35:16.123 回答