- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 C++ 中,您可以将许多内容声明为 constexpr
:变量、函数(包括成员函数和运算符)、构造函数,以及从 C++1z 开始,还有 if
statements和 lambda expressions .但是,声明 destructor constexpr
导致错误:
struct X {
constexpr ~X() = default; // error: a destructor cannot be 'constexpr'
};
我的问题:
constexpr
?constexpr
?~X() = default;
),它会自动constexpr
吗?最佳答案
根据 draft basic.types#10可能具有以下所有属性的 cv 限定类类型:
A possibly cv-qualified class type that has all of the following properties:
(10.5.1) - it has a trivial destructor,
(10.5.2) - it is either a closure type, an aggregate type, or has at least one constexpr constructor or constructor template (possibly inherited from a base class) that is not a copy or move constructor,
(10.5.3) - if it is a union, at least one of its non-static data members is of non-volatile literal type
(10.5.4) - if it is not a union, all of its non-static data members and base classes are of non-volatile literal types.
问题一:为什么不能将析构函数标记为 constexpr?
因为只有平凡的析构函数才有资格使用 constexpr以下是 draft 的相关部分
A destructor is trivial if it is not user-provided and if:
(5.4) — the destructor is not virtual,
(5.5) — all of the direct base classes of its class have trivial destructors, and
(5.6) — for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.
Otherwise, the destructor is non-trivial.
问题2:如果我不提供析构函数,隐式生成的析构函数是constexpr吗?
是的,因为隐式生成的析构函数是普通类型,所以符合constexpr的条件
问题3:如果我声明了一个默认析构函数(~X() = default;),它会自动constexpr吗?
事实上,这个析构函数是用户声明和隐式生成的,因此它符合 constexpr 的条件。
我找不到任何直接引用,只有微不足道的 destructors
符合 constexpr
但如果析构函数不是微不足道的,那么它肯定是类类型不是 cv-qualified。
所以它有点隐含,因为你不能为 cv-qualified
类定义 destructor
。
自 C++20 起,用户定义的析构函数在某些条件下也可以是 constexpr。
The definition of a constexpr function shall satisfy the following requirements:
- its return type (if any) shall be a literal type;
- each of its parameter types shall be a literal type;
- it shall not be a coroutine ([dcl.fct.def.coroutine]);
- if the function is a constructor or destructor, its class shall not have any virtual base classes;
- its function-body shall not enclose ([stmt.pre])
- a goto statement,
- an identifier label ([stmt.label]),
- a definition of a variable of non-literal type or of static or thread storage duration.
关于c++ - 为什么不能将析构函数标记为 constexpr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45071876/
在 ubuntu gcc 8.0 中: void bar(){} constexpr int foo(int a) { if (a <=0 ) bar(); retur
考虑一个在运行时只包装一个值的类: template class NonConstValue { public: NonConstValue(const Type& val)
在试验 constexpr 函数和模板(以及非类型模板参数)时,我偶然发现了一个现象,我无法理解是哪条规则使它生效。 所以根据 constexpr-s 的规则,我的问题本质上是“为什么会发生这种情况”
我正在阅读 Nicolai M. Josuttis 所著的“C++ 17 The Complete Guide”一书,无法理解以下示例 auto squared1 = [](auto val) con
(使用 g++ 7.0 主干。) 给定以下“类型到值包装”实用程序... template struct type_wrapper { using type = T; }; // "Wraps" a
我编写了一些代码,它能够根据调用站点提供与给定函数关联的字符串(通过函数指针和并行数组的tuple)来分派(dispatch)给函数。 dispatch 函数不直接接受字符串,而是接受 Callabl
如果我想使用一些方便的东西,比如 make_array 我没有机会先声明我的数组,然后再像“早些时候”那样进行定义,因为我的 var 类型不可用定义前。 所以我找到了这个答案: Undefined r
使用 gcc (HEAD 7.0.0 201612) 我惊讶地发现这有效: constexpr long value(const char *definition) { if (definit
我有这个片段。 #include #include struct JustStr { JustStr(const std::string& x) : val(x) {} stati
我找不到任何关于新 C++17 if 初始化语法的信息和“constexpr if”在: http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p01
考虑以下函数: template auto concatenate(std::array &data1, std::array &data2) { std::array result;
假设我有以下对象: #include class Foo { public: constexpr Foo() {}; constexpr std::string foo() cons
我正在尝试使用 https://github.com/gdelugre/literal_ipaddr它说它是一个 C++17 constexpr implementation of inet_addr
我想重新定义unique_ptr用一个特殊的析构函数。因此,我使用以下代码尝试模仿 unique_ptr 的一些构造函数.遗憾constexpr施 worker 员拒绝 build ,我不知道为什么。
我想用结构名称的哈希值初始化一个结构成员。 constexpr uint32_t myHash(const char* const data) { //Some code for hash r
我正在尝试编译 C++ 库(使用 gcc 5.3.1-14ubuntu2)并遇到此类错误: > In file included from > /root/pitchfork/workspace/un
设置: 我有一个使用 SIMD 内部函数的函数,我想在一些 constexpr 函数中使用它。 为此,我需要将其设为 constexpr。但是,SIMD 内在函数没有标记为 constexpr,编译器
这是一个简化的代码示例,旨在生成任意值序列(在 std::iota 的意义上)和在它们之上的不同类别的迭代器: struct delta { template void inc(I&
考虑以下函数: template auto concatenate(std::array &data1, std::array &data2) { std::array result;
我偶然发现了调用非 constexpr 函数的 constexpr 模板函数:在以下代码段中,由于调用了非 constexpr set,bar 无法按预期编译,但 foo 可以编译。谁能告诉我 foo
我是一名优秀的程序员,十分优秀!