C ++ 0x decltype和范围解析运算符 [英] C++0x decltype and the scope resolution operator

查看:128
本文介绍了C ++ 0x decltype和范围解析运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Foo这样的类:

  struct Foo {static const int i = 9; }; 

我发现GCC 4.5将拒绝以下内容

  Foo f; 
int x = decltype(f):: i;

如果我使用中间typedef,它将起作用,例如:

  typedef decltype(f)ftype; 
int x = ftype :: i;

但是我更喜欢保持命名空间整洁。我认为优先顺序可能是个问题,因此我也尝试使用括号,但没有运气。

解决方案

它是有效的C ++ 0x,是不可能的吗?

解决方案

decltype(f):: i 。 GCC尚不支持。您可以使用身份模板来解决它

  template< typename T>结构体身份{typedef T type; }; 
int x = identity< decltype(f)> :: type :: i;

身份是<$ c的一部分$ c> boost :: mpl 命名空间。


With a class such as Foo:

struct Foo { static const int i = 9; };

I find that GCC 4.5 will reject the following

Foo f;
int x = decltype(f)::i;

It will work if I use an intermediate typedef, such as:

typedef decltype(f) ftype;
int x = ftype::i;

but I prefer to keep the namespace clean. I thought precedence may be an issue, so I've also tried parentheses, but no luck. Is it impossible as presented, or is there a piece of syntax that can help me?

解决方案

It is valid C++0x to say decltype(f)::i. GCC just doesn't support it yet. You can work it around with an identity template

template<typename T> struct identity { typedef T type; };
int x = identity<decltype(f)>::type::i;

identity is part of the boost::mpl namespace.

这篇关于C ++ 0x decltype和范围解析运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆