关于 C++11 中“统一初始化语法”的问题。
使用 C++11 中的下一个语法初始化结构是否合法(查看第 #128-137 行)?还是 POD 仍然是实际的?
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(); }
};
}