问题描述
回答我如何写一个看起来像方法的lambda表达式?,我试图利用以下事实将无捕获的lambda转换为成员函数指针:自C ++ 17起,无捕获的lambda具有 constexpr 将转换运算符转换为其函数指针类型.
While answering How do I write a lambda expression that looks like a method?, I tried to turn a captureless lambda into a member function pointer by exploiting the fact that, since C++17, captureless lambdas have a constexpr conversion operator to their function pointer type.
所以我想到一个问题,直到:
So I came up with an issue boiling down to:
template<void(*)()> struct A{};
int main()
{
A<static_cast<void(*)()>([]{})>{}; // 1
constexpr auto fp = static_cast<void(*)()>([]{});
A<fp>{}; // 2
}
现在,此代码以clang编译(自5.0.0版本开始),但gcc(> = 7.2)抱怨:
Now, this compiles in clang (since 5.0.0) but gcc(>=7.2) complains:
error: lambda-expression in template-argument
A<static_cast<void(*)()>([]{ /*whatever*/ })>{}; // 1
^
error: 'main()::<lambda()>::_FUN' is not a valid template argument for type 'void (*)()' because 'static constexpr void main()::<lambda()>::_FUN()' has no linkage
A<fp>{}; // 2
问题是,谁是对的?
推荐答案
这是gcc错误,已提交 83258 .
This is a gcc bug, filed 83258.
在C ++ 14中,我们曾经有一个链接指针类型的非类型模板参数的必要条件.但是在C ++ 17中(由于 N4268 ),则该参数只需将转换即可正确类型的常量表达式,但有其他一些限制(此处没有任何限制).一旦构造了fp
,就应该能够将其用作模板参数.
In C++14, we used to have a linkage requirement for non-type template parameters of pointer type. But in C++17 (as a result of N4268), the parameter just needs to be a converted constant expression of the correct type, with a few other restrictions (none of which are relevant here). Once we can construct fp
, we should be able to use it as a template parameter.
这篇关于我可以将C ++ 17不捕获lambda constexpr转换运算符的结果用作函数指针模板非类型参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!