我有一个示例代码如下。为什么我的构造函数允许使用临时对象调用非const引用作为参数?
#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引用上面的代码,它虽然有趣功能无法编译编译罚款。这是特定于g ++编译器还是C++标准需要说些什么?
编辑:
g ++ -v给出了这个。
gcc版本4.5.2(Ubuntu的/ Linaro的4.5.2-8ubuntu4)
++您正在使用您应指定克的版本。 – Gabe 2011-06-07 16:11:00