【发布时间】:2011-02-09 21:25:32
【问题描述】:
此代码可在 CodeGear 2009 和 Visual Studio 2010 中编译,但不能在 gcc 中编译。为什么?
class Foo
{
public:
operator int() const;
template <typename T> T get() const { return this->operator T(); }
};
Foo::operator int() const
{
return 5;
}
错误信息是:
test.cpp:在成员函数`T Foo::get() const'中:
test.cpp:6:错误:“const class Foo”没有名为“operator T”的成员
【问题讨论】:
-
好吧,如果您要求转换为
int以外的其他内容,它将不起作用。你是怎么用的? -
我什至还没到可以使用它的地步……它在那之前就坏了。
-
你在任何地方都调用 Foo::get() 吗?
标签: c++ templates casting operator-overloading