Unified initialization syntax in C++11

1.6k Views Asked by At

The question about “unified initialization syntax” in C++11.

Is it legal to initialize the struct with the next syntax in C++11 (look at lines #128-137)? Or POD is still actual?

http://pastebin.com/GMZ5QDmR

The problem with MSVC 2013 compiler. This example compiles successfully, but crashes with bad function call exception. That told to me the std::function object not initialized properly.

By the way, the ICC 13.0 can not compile the code in the example.

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

Is it defect in compilers? Or with compilers all is ok, and such approach is not compliant with C++11?

Here is short example:

#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(); }
    };
}
1

There are 1 best solutions below

0
On

First: void main is illegal in C++. Use int main.

Microsoft Visual Studio 2013 isn't the star of the C++11 class. Neither is Intel's compiler version 13, because, well, they have version 14. That being said, you can try the MSVC++ 2013 November CTP, which is a tiny bit better on the language support.

If you want to see if your code is valid, use the latest version of GCC or Clang, both available on the Greatest Online Compiler Platform in Existence.

The C++ language features are reported on the vendor's websites:

Any other C++ compiler isn't worth mentioning wrt C++11.

Note that the Standard library features may not be mentioned on these pages.

The feature you are looking usually falls under "(general) initializer lists".