Can I use the result of a C++17 captureless lambda constexpr conversion operator as a function pointer template non-type argument?(我可以将 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.
这篇关于我可以将 C++17 无捕获 lambda constexpr 转换运算符的结果用作函数指针模板非类型参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以将 C++17 无捕获 lambda constexpr 转换运算符的


基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01