C++ 中的可移植 typeof()使用SFINAE?
可能的重复:
C++03 中缺少 typeof 运算符?
是否可以使用模板在 C++0x 之前的 C++ 中可移植获取表达式的编译时类型?
类似于:
int foo() { return 0; } //Let's say we have this
typeof(foo()) x; //x would now be of type 'int'
但由于我们正在使用模板,因此显然可能需要稍微不同的语法。
(我在这里和网络上的其他地方看到了许多重复项,但它们似乎依赖于特定于编译器的功能或依赖于 C++0x(C++11?)decltype
功能。)
Possible Duplicate:
Absence of typeof operator in C++03?
Is it possible to use templates to get the compile-time type of an expression portably in pre-C++0x C++?
Something like:
int foo() { return 0; } //Let's say we have this
typeof(foo()) x; //x would now be of type 'int'
but since we're working with templates, slightly different syntax might be needed obviously.
(I've seen many duplicates here and elsewhere on the web, but they either seem to rely on compiler-specific features or on C++0x (C++11?) decltype
features.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般情况下是不可能的。是的,多年来我们为解决这个问题付出了很大的努力。
在特定情况下确定表达式的类型有很多技巧(例如,函子通常用 typedef 进行注释以指示其返回类型),但一般情况下,你需要
decltype
,这正是它被添加到C++0x中的原因。标准委员会遵循的政策是不添加核心语言功能来完成图书馆本来可以做的事情。It's impossible in the general case. And yes, quite a lot of effort has been put into this problem over the years.
There are a lot of tricks for determining the type of an expression in specific cases (for example, functors are commonly annotated with a typedef indicating their return type), but in general, you need
decltype
, which is precisely why it was added in C++0x. The standards committee follows a policy of not adding core language features to do something a library could have done just as well.我不知道实现,但看看 Boost。类型。
I don't know the implementation, but take a look at Boost.Typeof.
这是不可能。您可以使用函数/方法执行的唯一编译时操作是
sizeof()
,它无助于推导类型(如typeid
)。It's NOT possible. The only compile time operation you can perform with a function/method is
sizeof()
, which doesn't help deducing a type (liketypeid
).