我可以将 C++17 无捕获 lambda constexpr 转换运算符的结果用作函数指针模板非类型参数吗?
在回答我该怎么做编写一个看起来像方法的 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.
相关文章