小编Mil*_*zka的帖子

decltype和C++中的作用域运算符

我需要获取实例化模板时提供的类型.请考虑以下示例:

template <typename T> struct Foo
{
  typedef T TUnderlying;
};

static Foo<int> FooInt;

class Bar
{
public:
  auto Automatic() -> decltype(FooInt)::TUnderlying
  {
    return decltype(FooInt)::TUnderlying();
  }
};

int main()
{
  Bar bar;
  auto v = bar.Automatic();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码的问题是将范围运算符与decltype一起使用.Visual C++ 2010抱怨如下:

错误C2039:'TUnderlying':不是'`global namespace''的成员

我在维基百科上收集了一些关于这个主题的信息:

在评论C++ 0x的正式委员会草案时,日本ISO成员机构指出"范围运算符(::)不能应用于decltype,但它应该是.在获取成员类型的情况下将是有用的(嵌套类型)来自实例,如下所示":[16]

vector<int> v;
decltype(v)::value_type i = 0; // int i = 0;
Run Code Online (Sandbox Code Playgroud)

David Vandevoorde解决了这个和类似的问题,并于2010年3月投票进入了工作文件.

所以我认为Visual C++ 2010没有实现这一点.我想出了这个解决方法:

template <typename T> struct ScopeOperatorWorkaroundWrapper
{
  typedef typename T::TUnderlying TTypedeffedUnderlying;
};

auto Automatic() -> ScopeOperatorWorkaroundWrapper<decltype(FooInt)>::TTypedeffedUnderlying
{
  return …
Run Code Online (Sandbox Code Playgroud)

c++ scope decltype operator-keyword c++11

15
推荐指数
1
解决办法
1998
查看次数

标签 统计

c++ ×1

c++11 ×1

decltype ×1

operator-keyword ×1

scope ×1