G++ 4.9.2 传递引用'this'时的回归
g++ 4.9.2 regression on pass reference to 'this'
这是指向实现代码的指针的最小化部分:
template<typename T>
class PImpl {
private:
T* m;
public:
template<typename A1>
PImpl(A1& a1) : m(new T(a1)) {
}
};
struct A{
struct AImpl;
PImpl<AImpl> me;
A();
};
struct A::AImpl{
const A* ppub;
AImpl(const A* ppub)
:ppub(ppub){}
};
A::A():me(this){}
A a;
int main (int, char**){
return 0;
}
它可在G++4.8及以前版本上编译,也能正常工作。但是G++4.9.2编译器会引发以下错误:
prog.cpp: In constructor 'A::A()':
prog.cpp:24:15: error: no matching function for call to 'PImpl<A::AImpl>::PImpl(A*)'
A::A():me(this){}
^
prog.cpp:24:15: note: candidates are:
prog.cpp:9:5: note: PImpl<T>::PImpl(A1&) [with A1 = A*; T = A::AImpl]
> PImpl(A1& a1) : m(new T(a1)) {
^
prog.cpp:9:5: note: no known conversion for argument 1 from 'A*' to 'A*&'
prog.cpp:2:7: note: PImpl<A::AImpl>::PImpl(const PImpl<A::AImpl>&)
class PImpl {
^
prog.cpp:2:7: note: no known conversion for argument 1 from 'A*' to 'const PImpl<A::AImpl>&'
但它可以通过小黑客来修复。如果我通过'&this"而不是"this,然后它进入可编译状态。
是G++回归还是新的C++标准功能消除了向后兼容性?
我们可以制作一个更简单的例子,既不在g++4.9上编译,也不在clang:上编译
template <typename T>
void call(T& ) { }
struct A {
void foo() { call(this); }
};
int main()
{
A().foo();
}
这是因为从标准来看,this
是[类别this](§9.3.2):
在非静态(9.3)成员函数的主体中,关键字
this
是prvalue表达式,其值是为其调用函数的对象的地址。
您不能将左值引用到prvalue,因此会出现错误-在这种情况下,gcc比clang更能解释这个错误:
错误:从
A*
类型的右值初始化A*&
类型的非常数引用无效
如果我们将call
重写为const T&
或T&&
,那么两个编译器都接受代码。
这不是用gcc-4.6编译的,所以似乎gcc-4.8是回归发生的地方。你想要的似乎是用通用参考来取A1
,即:PImpl(A1 && a1)
。这为我编译了gcc-4.6、gcc-4.8和gcc-4.9。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么使用 "this" 指针调用派生成员函数?
- C++错误:"error: int aaa::bbb is protected within this context"
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 关于C++中具有多重继承"this"指针的说明
- 在noexcept 规范中是否允许使用"this"?
- 如何修复"error: ‘_1’ was not declared in this scope"?
- C++调用具有 *this 属性的单个帮助程序函数
- Doees the 'this' 指针参与虚函数的多态行为
- 在 c++ 中正确定义"this"关键字?
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- QObject::连接无法将信号连接到*this*对象的插槽
- 析构函数中的"delete this"
- 为什么成员函数内的"this"指针为空?
- 为什么我需要在转换构造函数上引用 this->?
- "this"指针的值在对象的生存期内是否恒定?
- C++ "Invalid use of 'this' in non-member function" ,
- G++ 4.9.2 传递引用'this'时的回归