[英]Should lambda decay to function pointer in templated code?
I read somewhere that a lambda function should decay to function pointer if the capture list is empty. 我读到某个地方,如果捕获列表为空,lambda函数应该衰减到函数指针。 The only reference I can find now is n3052 .
我现在唯一能找到的参考是n3052 。 With g++ (4.5 & 4.6) it works as expected, unless the lambda is declared within template code.
使用g ++(4.5和4.6)它可以按预期工作,除非lambda在模板代码中声明。
For example the following code compiles: 例如,以下代码编译:
void foo() {
void (*f)(void) = []{};
}
But it doesn't compile anymore when templated (if foo
is actually called elsewhere): 但它在模板化时不再编译(如果
foo
实际上在其他地方调用):
template<class T>
void foo() {
void (*f)(void) = []{};
}
In the reference above, I don't see an explanation of this behaviour. 在上面的参考中,我没有看到这种行为的解释。 Is this a temporary limitation of g++, and if not, is there a (technical) reason not to allow this?
这是g ++的临时限制,如果没有,是否有(技术)理由不允许这样做?
I can think of no reason that it would be specifically disallowed. 我认为没有理由不被特别禁止。 I'm guessing that it's just a temporary limitation of g++.
我猜这只是g ++的暂时限制。
I also tried a few other things: 我还尝试了一些其他的东西:
template <class T>
void foo(void (*f)(void)) {}
foo<int>([]{});
That works. 这样可行。
typedef void (*fun)(void);
template <class T>
fun foo() { return []{}; } // error: Cannot convert.
foo<int>()();
That doesn't (but does if foo
is not parameterized). 那不是(但如果
foo
没有参数化的话)。
Note: I only tested in g++ 4.5. 注意:我只在g ++ 4.5中测试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.