简体   繁体   English

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

[英]Why does `return {};` not apply to `std::forward_list`?

My compiler is clang 3.4, which completely supports C++14 and std::forward_list . 我的编译器是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 {}; // ???
}

Why does return {}; 为什么要return {}; not apply to std::forward_list ? 不适用于std::forward_list

Well, even if your compiler is C++14-compliant, your standard library isn't :) 好吧,即使您的编译器符合C ++ 14标准,您的标准库也不是:)

C++11 has: C ++ 11具有:

explicit forward_list( const Allocator& alloc = Allocator() );

whereas C++14 has (since library DR2193 ): 而C ++ 14有(自库DR2193 ):

forward_list() : forward_list( Allocator() ) {}
explicit forward_list( const Allocator& alloc );

If you change A 's default constructor to explicit A(char* = nullptr) you'll see the same behavior. 如果将A的默认构造函数更改为explicit A(char* = nullptr)您将看到相同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM