我在下面有一个示例代码。
#include<iostream>
template<typename T>
class XYZ
{
private:
T & ref;
public:
XYZ(T & arg):ref(arg)
{
}
};
class temp
{
int x;
public:
temp():x(34)
{
}
};
template<typename T>
void fun(T & arg)
{
}
int main()
{
XYZ<temp> abc(temp());
fun(temp()); //This is a compilation error in gcc while the above code is perfectly valid.
}
在上面的代码中,即使 XYZ 构造函数将参数作为非 const 引用,它也可以正常编译,而 fun 函数无法编译。这是特定于 g++ 编译器还是 c++ 标准必须对此有所说明?
编辑:
g++ -v 给出了这个。
gcc 版本 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)