Search code examples
c++c++11list-initializationaggregate-initialization

Default value of function parameter initialized by list initialization


Could anyone help me with the following problem?

There is a simple code:

#include <vector>

struct A {
    std::vector<int> vec;
};

void func (A &&a = {}) {}

int main()
{
    func();
    return 0;
}

When I try to compile it by gcc 5.4.0 I get the error:

undefined reference to `std::vector<int, std::allocator<int> >::vector()'

Amazingly, but clang compiles it well. Also if to modify the code a little bit it is compiled without any problems:

#include <vector>

struct A {
    std::vector<int> vec;
};

void func (A &&a) {}

int main()
{
    func({});
    return 0;
}

I really cann't understand what's wrong with the first code.


Solution

  • This is a gcc bug. It can also be reproduced with

    template<typename Value>
    struct A
    {
        A() = default;
        std::vector<Value> m_content;
    };
    
    void func(A<int> a = {})
    {
    }
    
    int main()
    {
        func();
    }
    

    Currently though, there is no status on it.

    I appears that the lack of an actual instance of the vector is causing the compiler to not stamp out the code for it which leads to the undefined reference.