模板,类型名,lambda - >依赖名称不依赖? [英] templates, typename, lambda -> dependent names not dependent?

查看:182
本文介绍了模板,类型名,lambda - >依赖名称不依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

template < typename Something >
boost::function<void()> f()
{
  typedef typename Something::what type;
  return [](){};
}

在这段代码中,你需要typename,因为'what'但请考虑:

In this code you need the typename because 'what' is a dependent name. But consider this:

template < typename Something >
boost::function<void()> f()
{
  return []()
  { 
    typedef typename Something::what type;
  };
}

编译程序bitches:typename不能在模板声明外使用

Compiler bitches: "typename cannot be used outside a template declaration"

WTF?

这项工作:

template < typename Something >
boost::function<void()> f()
{
  return []()
  { 
    typedef Something::what type;
  };
}

关于lambda的创建意味着什么一个依赖名称了吗?或者这只是一个错误?

What is it about the creation of a lambda that means "what" is not a dependent name anymore? Or is this just a bug?

嘿...修正。后者不工作。它说,东西不存在。这个修改版本的DOES工作,但仍然不直接不需要,不会接受typename。

Heh...correction. The latter doesn't work. It says that "Something" doesn't exist. This modified version DOES work though and still unintuitively doesn't need and won't accept "typename".

template < typename T > struct wtf { typedef typename T::what type; };

template < typename Something >
boost::function<void()> f()
{
  return []() { typedef wtf<Something>::type type; };
}



当然,现在我有两个问题:原来的,WTF doesn'除非用作模板参数,否则找到Something。

Of course, now I have TWO questions: the original and, WTF doesn't it find "Something" unless it's used as a template parameter??

推荐答案

这是一个非常有趣的问题。从我的理解,第一个WTF(在lambda主体中 typename 的一个)应该是正确的根据N3225 5.1.2 / 7:

That's a very interesting question. From my understanding, the first 'WTF' (the one with typename in the lambda body) should be the correct according to N3225 5.1.2/7 :


复合语句会生成 function-body <函数调用运算符
,但是出于名称查找的目的,确定此类型和值并转换 id表达式
对于非静态类成员到类成员访问表达式中使用(* this),
复合语句考虑在 lambda-

The lambda-expression’s compound-statement yields the function-body of the function call operator, but for purposes of name lookup, determining the type and value of this and transforming id-expressions referring to non-static class members into class member access expressions using (*this), the compound-statement is considered in the context of the lambda-expression.

由于在lambda表达式的上下文中,它应该也是在根据这个引号的lambda函数体的上下文中的依赖名称。

As Something is a dependent-name in the context of the lambda expression, it should also be a dependent name in the context of the lambda function body according to this quote.

这篇关于模板,类型名,lambda - &gt;依赖名称不依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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