c++11 参数包错误行为与 Apple LLVM 7.0.0 但适用于 GCC-5.1
c++11 parameter pack wrong behaviour with Apple LLVM 7.0.0 but works with GCC-5.1
从这个问题的先前版本开始,感谢@Gene,我现在能够使用一个更简单的示例重现此行为。
#include <iostream>
#include <vector>
class Wrapper
{
std::vector<int> const& bc;
public:
Wrapper(std::vector<int> const& bc) : bc(bc) { }
int GetSize() const { return bc.size(); }
};
class Adapter
{
Wrapper wrapper;
public:
Adapter(Wrapper&& w) : wrapper(w) { }
int GetSize() const { return wrapper.GetSize(); }
};
template <class T>
class Mixin : public Adapter
{
public:
//< Replace "Types ... args" with "Types& ... args" and it works even with Apple LLVM
template <class ... Types>
Mixin(Types ... args) : Adapter(T(args...)) { }
};
int main()
{
std::vector<int> data;
data.push_back(5);
data.push_back(42);
Mixin<std::vector<int>> mixin(data);
std::cout << "data: " << data.size() << "\n";
std::cout << "mixin: " << mixin.GetSize() << "\n";
return 0;
}
使用 Apple LLVM 的结果,使用 -std=c++11
和 -std=c++14
测试:
data: 2
mixin: -597183193
有趣的是,我还测试了这段代码 @ideone,它使用启用了 C++14 的 gcc-5.1,它按预期工作!
data: 2
mixin: 2
为什么 mixin.GetSize()
return 是 Clang 上的垃圾值,为什么它适用于 GCC-5.1?
@Gene 建议我使用 Types ... args
创建矢量的临时副本(并使用 Types& ... args
使其与 LLVM 一起使用),但该副本将包含相同的元素(因此也具有相同的大小)。
你有一个悬空引用,mixin.GetSize()
产生 undefined behavior:
- 在
Mixin
的构造函数内部,T
= std::vector<int>
,因此 Adapter(T(args...))
正在向 Adapter
的构造函数传递一个临时的 std::vector<int>
Adapter
的构造函数参数是一个Wrapper&&
,但是我们传递给它一个std::vector<int>&&
,所以我们调用Wrapper
的隐式转换构造函数
Wrapper
的构造函数参数是一个std::vector<int> const&
,我们传递给它一个std::vector<int>&&
;右值被允许绑定到 const-lvalue 引用,所以这 在语法上 很好并且编译很好,但实际上我们将 Wrapper::bc
绑定到一个临时的 [=55] =]
- 一旦构建完成,在
Mixin
的构造函数中创建的临时对象的生命周期结束,Wrapper::bc
成为悬空引用;调用 Adapter::GetSize
现在产生 UB
当Mixin
的构造函数参数由Types...
变为Types&...
时,Adapter(T(args...))
仍然传递Adapter
的构造函数是临时的std::vector<int>
;它只有 出现 可以工作,因为您看到了 UB 的不同表现形式(可能堆栈看起来有点不同,因为 std::vector<int>
的副本减少了)。即,两个 版本的代码同样是 broken/wrong!
所以,具体回答这个问题:
Why does mixin.GetSize()
return a garbage value on Clang and why does it work with GCC-5.1?
因为未定义行为的行为是未定义的。 ;-] 看起来工作是一种可能的结果,但代码仍然是错误的,正确的外观纯粹是肤浅的。
从这个问题的先前版本开始,感谢@Gene,我现在能够使用一个更简单的示例重现此行为。
#include <iostream>
#include <vector>
class Wrapper
{
std::vector<int> const& bc;
public:
Wrapper(std::vector<int> const& bc) : bc(bc) { }
int GetSize() const { return bc.size(); }
};
class Adapter
{
Wrapper wrapper;
public:
Adapter(Wrapper&& w) : wrapper(w) { }
int GetSize() const { return wrapper.GetSize(); }
};
template <class T>
class Mixin : public Adapter
{
public:
//< Replace "Types ... args" with "Types& ... args" and it works even with Apple LLVM
template <class ... Types>
Mixin(Types ... args) : Adapter(T(args...)) { }
};
int main()
{
std::vector<int> data;
data.push_back(5);
data.push_back(42);
Mixin<std::vector<int>> mixin(data);
std::cout << "data: " << data.size() << "\n";
std::cout << "mixin: " << mixin.GetSize() << "\n";
return 0;
}
使用 Apple LLVM 的结果,使用 -std=c++11
和 -std=c++14
测试:
data: 2
mixin: -597183193
有趣的是,我还测试了这段代码 @ideone,它使用启用了 C++14 的 gcc-5.1,它按预期工作!
data: 2
mixin: 2
为什么 mixin.GetSize()
return 是 Clang 上的垃圾值,为什么它适用于 GCC-5.1?
@Gene 建议我使用 Types ... args
创建矢量的临时副本(并使用 Types& ... args
使其与 LLVM 一起使用),但该副本将包含相同的元素(因此也具有相同的大小)。
你有一个悬空引用,mixin.GetSize()
产生 undefined behavior:
- 在
Mixin
的构造函数内部,T
=std::vector<int>
,因此Adapter(T(args...))
正在向Adapter
的构造函数传递一个临时的std::vector<int>
Adapter
的构造函数参数是一个Wrapper&&
,但是我们传递给它一个std::vector<int>&&
,所以我们调用Wrapper
的隐式转换构造函数Wrapper
的构造函数参数是一个std::vector<int> const&
,我们传递给它一个std::vector<int>&&
;右值被允许绑定到 const-lvalue 引用,所以这 在语法上 很好并且编译很好,但实际上我们将Wrapper::bc
绑定到一个临时的 [=55] =]- 一旦构建完成,在
Mixin
的构造函数中创建的临时对象的生命周期结束,Wrapper::bc
成为悬空引用;调用Adapter::GetSize
现在产生 UB
当Mixin
的构造函数参数由Types...
变为Types&...
时,Adapter(T(args...))
仍然传递Adapter
的构造函数是临时的std::vector<int>
;它只有 出现 可以工作,因为您看到了 UB 的不同表现形式(可能堆栈看起来有点不同,因为 std::vector<int>
的副本减少了)。即,两个 版本的代码同样是 broken/wrong!
所以,具体回答这个问题:
Why does
mixin.GetSize()
return a garbage value on Clang and why does it work with GCC-5.1?
因为未定义行为的行为是未定义的。 ;-] 看起来工作是一种可能的结果,但代码仍然是错误的,正确的外观纯粹是肤浅的。