c++ - static constexpr 函数在模板结构中工作,但不在结构中。为什么?
问题描述
以下编译:
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/binary/binary.hpp>
namespace x3 = boost::spirit::x3;
template <int dummy=0>
struct S {
static constexpr auto get_parse_rule() {
return x3::byte_ >> x3::byte_;
}
};
int main() {
auto parse_rule = S<>::get_parse_rule();
return 0;
}
但这不会:
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/binary/binary.hpp>
namespace x3 = boost::spirit::x3;
struct S {
static constexpr auto get_parse_rule() {
return x3::byte_ >> x3::byte_;
}
};
int main() {
auto parse_rule = S::get_parse_rule();
return 0;
}
当我阅读这两篇文章时,我看到了相同的东西,但显然它们并不相同。有人可以告诉我为什么会这样吗?
解决方案
Constexpr 函数的返回类型必须是LiteralType。升压精神会返回不满足这些要求的东西。Constexpr 函数模板实例化在不满足要求时可能会编译,但无论如何您都不会从中获得编译时间常量。
推荐阅读
- javascript - 预渲染 d3 和关键函数
- javascript - 我从页面速度洞察中收到“预加载密钥请求”的警告,但无法解决问题
- javascript - 如何使用仅允许名称中的字符的php验证表单
- postgresql - 约束:只有一行必须有 NULL 值
- javascript - 使用 spring-boot 和 chart js 可视化 MySQL 数据库中的数据
- graphql - 当将参数作为外部变量传递时,Graphql 指令不适用于突变输入参数
- c++ - double 或 float 类型数据自动四舍五入
- powershell - 使用 powershell 跳过受密码保护的 powerpoint 文件
- python - SAM 应用程序有效,但运行 pytest 会抛出...ModuleNotFoundError: No module named 'aurora'
- python - 有没有更好的方法在使用字典而不是 set() 时从列表中删除重复值,以保留值插入顺序?