由列表初始化初始化的函数参数的默认值

时间:2022-03-30 17:08:42

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:

当我尝试通过gcc 5.4.0编译它时,我收到错误:

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:

令人惊讶的是,但clang编译得很好。此外,如果稍微修改代码,它编译没有任何问题:

#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.

我真的不明白第一个代码有什么问题。

2 个解决方案

#1


11  

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

这是一个gcc bug。它也可以复制

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.

我似乎缺少一个实际的向量实例导致编译器没有删除它的代码,导致未定义的引用。

#2


3  

As @NathanOliver mentioned, this is a GCC bug. What is the bug? It seems to me that it forgets to instantiate a class std::vector<int> when it is used as a function parameter and enclosed in another type.
You can work around it by using A somewhere else in the code. Even a simple declaration is enough. It forces the compiler to instantiate the templated type and you are good. Look for A unused in the code below that will compile.

正如@NathanOliver所提到的,这是一个GCC错误。什么是虫子?在我看来,当它被用作函数参数并包含在另一个类型中时,它会忘记实例化一个类std :: vector 。您可以通过在代码中的其他位置使用A来解决它。即使是简单的声明也足够了。它强制编译器实例化模板化类型,你很好。在下面的代码中查找未编译的A将被编译。

#include <vector>

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

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

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

#1


11  

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

这是一个gcc bug。它也可以复制

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.

我似乎缺少一个实际的向量实例导致编译器没有删除它的代码,导致未定义的引用。

#2


3  

As @NathanOliver mentioned, this is a GCC bug. What is the bug? It seems to me that it forgets to instantiate a class std::vector<int> when it is used as a function parameter and enclosed in another type.
You can work around it by using A somewhere else in the code. Even a simple declaration is enough. It forces the compiler to instantiate the templated type and you are good. Look for A unused in the code below that will compile.

正如@NathanOliver所提到的,这是一个GCC错误。什么是虫子?在我看来,当它被用作函数参数并包含在另一个类型中时,它会忘记实例化一个类std :: vector 。您可以通过在代码中的其他位置使用A来解决它。即使是简单的声明也足够了。它强制编译器实例化模板化类型,你很好。在下面的代码中查找未编译的A将被编译。

#include <vector>

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

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

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