C++ 中的可移植 typeof()使用SFINAE?

发布于 2024-11-10 17:57:10 字数 523 浏览 9 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱娆 2024-11-17 17:57:10

一般情况下是不可能的。是的,多年来我们为解决这个问题付出了很大的努力。

在特定情况下确定表达式的类型有很多技巧(例如,函子通常用 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.

想念有你 2024-11-17 17:57:10

我不知道实现,但看看 Boost。类型

I don't know the implementation, but take a look at Boost.Typeof.

女中豪杰 2024-11-17 17:57:10

这是不可能。您可以使用函数/方法执行的唯一编译时操作是 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 (like typeid).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文