Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My compiler is clang 3.4, which completely supports C++14 and 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 {}; not apply to std::forward_list?

like image 402
xmllmx Avatar asked Jul 01 '14 12:07

xmllmx


1 Answers

Well, even if your compiler is C++14-compliant, your standard library isn't :)

C++11 has:

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

whereas C++14 has (since library 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.

like image 136
ecatmur Avatar answered Nov 15 '22 08:11

ecatmur