静态 constexpr 函数在模板结构中工作,但不能在结构中工作.为什么?
static constexpr function work in template struct but not struct. Why?
以下编译:
#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 功能模板实例化可以在不满足要求时进行编译,但无论如何您都不会从中获得编译时常量。
相关文章:
- 如何使我的 sizeof sum 结构与空参数包一起工作
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- 在下面的示例中,结构名称和函数如何工作?
- <string> 结构中的矢量无法正常工作
- 静态 constexpr 函数在模板结构中工作,但不能在结构中工作.为什么?
- 试图使模板用于C 中结构的元素工作
- 指向成员结构的指针如何工作
- 字频 strcmp 使用结构数组无限工作
- 这个 c++ 结构是如何工作的,我如何使用它
- 结构在C++中如何工作
- C++:无法使套接字、螺纹和结构工作
- cudaMemcpy 结构设备主机不工作
- 返回一个结构数组(部分工作)C++
- 现代C++.从继续执行的工作线程返回数据结构
- "within"结构 C/C++ 工作
- 此C/C 的结构初始化器如何使用足够的字段工作
- 如何与CUDA中的结构内部结构一起工作
- 结构是如何工作的
- 工作线程队列的这种变体是某种模式或通用结构吗?
- 基于PHP示例,与C 中数据结构一起工作的多态性示例