为什么`return {};`不适用于`std :: forward_list`?

xml*_*lmx 6 c++ constructor compiler-errors clang c++11

我的编译器是clang 3.4,它完全支持C++ 14和std::forward_list.

#include <forward_list>

struct A
{
    A()
    {}

    explicit A(initializer_list<int>)
    {}
};

A f1()
{
    return A(); // OK
}

A f2()
{
    return {}; // OK
}

typedef std::forward_list<int> T;

T f3()
{
    return T(); // OK
}

T f4()
{
    // error : converting to 'T {aka std::forward_list<int>}' from initializer 
    // list would use explicit constructor 'std::forward_list'     
    return {}; // ???
}
Run Code Online (Sandbox Code Playgroud)

为什么不return {};适用std::forward_list

eca*_*mur 18

好吧,即使您的编译器符合C++ 14标准,您的标准库也不是:)

C++ 11具有:

explicit forward_list( const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)

而C++ 14有(自库DR2193):

forward_list() : forward_list( Allocator() ) {}
explicit forward_list( const Allocator& alloc );
Run Code Online (Sandbox Code Playgroud)

如果更改了A默认构造函数,explicit A(char* = nullptr)您将看到相同的行为.