我的编译器是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 {}; // ???
}
为什么要回归{};不适用于std :: forward_list?
解决方法
好吧,即使您的编译器符合C14标准,您的标准库也不是:)
C 11有:
explicit forward_list( const Allocator& alloc = Allocator() );
而C 14有(自library DR2193起):
forward_list() : forward_list( Allocator() ) {}
explicit forward_list( const Allocator& alloc );
如果将A的默认构造函数更改为显式A(char * = nullptr),您将看到相同的行为.