请看一下这段代码:
template<class T>
class A
{
class base
{
};
class derived : public A<T>::base
{
};
public:
int f(typename A<T>::base& arg = typename A<T>::derived())
{
return 0;
}
};
int main()
{
A<int> a;
a.f();
return 0;
}
编译在 g++ 中生成以下错误消息:
test.cpp: In function 'int main()':
test.cpp:25: error: default argument for parameter of type
'A<int>::base&' has type 'A<int>::derived'
基本思想(使用派生类作为基本引用类型参数的默认值)适用于 Visual Studio,但不适用于 g++。我必须将我的代码发布到大学服务器,在那里他们用 gcc 编译它。我能做些什么?我有什么遗漏的吗?
您无法创建对右值的(可变)引用。尝试使用常量引用:
int f(const typename A<T>::base& arg = typename A<T>::derived())
// ^^^^^
当然不能修改arg
带有常量引用。如果必须使用(可变)引用,请使用重载。
int f(base& arg) {
...
}
int f() {
derived dummy;
return f(dummy);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)