模板和铸造操作员
此代码可以在 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: In member function `T Foo::get() const':
test.cpp:6:错误:“const class Foo”没有名为“operator T”的成员
This code compiles in CodeGear 2009 and Visual Studio 2010 but not gcc. Why?
class Foo
{
public:
operator int() const;
template <typename T> T get() const { return this->operator T(); }
};
Foo::operator int() const
{
return 5;
}
The error message is:
test.cpp: In member function `T Foo::get() const':
test.cpp:6: error: 'const class Foo' has no member named 'operator T'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 G++ 中的一个错误。
operator T
是一个非限定的依赖名称(因为它有T
,因此查找将根据其类型而有所不同)。因此,在实例化时必须查找它。标准规则因此,在运算符关键字之后指定的类型名称不必以任何方式进行词法匹配。您可以应用以下解决方法来强制 GCC 将其视为从属名称
It's a bug in G++.
operator T
is an unqualified dependent name (because it hasT
in it and lookup will thus be different depending on its type). As such it has to be looked up when instantiating. The Standard rulesThus the type name specified after the operator keyword doesn't have to match lexically in any way. You can apply the following work-around to force GCC treating it as a dependent name
我不确定 C++ 运算符名称的确切规则是什么,但我相信它试图调用
operator T()
而不是operator int()
。为什么不直接使用强制转换:我还没有测试过这个,但我相信这或多或少会完成相同的事情。如果没有,应该有一种方法可以完成此任务,而无需直接调用
operator T()
。毕竟,这就是重载运算符的用途。I'm not sure what the exact rules for the names of C++ operators are, but I believe it's trying to call
operator T()
instead ofoperator int()
. Why not just use a cast:I haven't tested this but I believe this will accomplish more or less the same thing. If not, there should be a way to accomplish this without having to call
operator T()
directly. That's what overloaded operators are for, after all.如果我将代码稍微修改为:
我会通过 GCC 得到以下结果:
但如果您要使用 -fpermissive,整个事情只有在
T = int
时才有效。另一方面,为什么不这样做:
这就是转换运算符的要点。
If I modify the code slightly to this:
I get the following with GCC:
But if you were to use -fpermissive, the whole thing would only work if
T = int
On the other hand, why not do:
That's sort of the point of the conversion operator.
我对此的用例有点好奇。我想是这样,你可以说:
但为什么不说:
键入的时间稍长(尽管可以使用 C 转换表示法),但不需要任何跳圈。
I'm s bit curious about the use case for this. I suppose it is so you can say:
but why not say:
which is slightly longer to type (though one could use the C cast notation), but doesn't need any hoop jumping.