考虑:
template < typename Something >
boost::function<void()> f()
{
typedef typename Something::what type;
return [](){};
}
在这段代码中,您需要类型名称,因为“what”是从属名称。但考虑一下:
template < typename Something >
boost::function<void()> f()
{
return []()
{
typedef typename Something::what type;
};
}
编译器婊子们:“typename 不能在模板声明之外使用”
什么鬼?
这行得通:
template < typename Something >
boost::function<void()> f()
{
return []()
{
typedef Something::what type;
};
}
创建一个意味着“什么”不再是从属名称的 lambda 是什么意思?或者这只是一个错误?
嘿...更正。后者不起作用。它说“某物”不存在。这个修改后的版本确实有效,但仍然不直观地不需要也不会接受“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 不会找到“Something”,除非它被用作模板参数??
最佳答案
这是一个非常有趣的问题。根据我的理解,根据 N3225 5.1.2/7,第一个“WTF”(lambda 主体中带有 typename
的那个)应该是正确的:
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.
因为 Something
是 lambda 表达式上下文中的从属名称,根据此,它应该也是 lambda 函数体上下文中的从属名称报价。
关于c++ - templates, typename, lambda -> 依赖名称不依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4589056/