c++ - constexpr 和函数体 = 删除 : what's the purpose?

标签 c++ c++11 constexpr

根据[dcl.constexpr/3] :

The definition of a constexpr function shall satisfy the following requirements:
[...]
- its function-body shall be = delete, = default, or [...]

这意味着以下类片段是有效的:

struct S {
    constexpr void f() = delete;
};

删除 constexpr 函数的目的是什么?
定义它 constexpr 如果有的话,有什么好处?

我无法找出任何原因,但事实上,在标准中允许它可能比禁止它更容易。

最佳答案

这是基于CWG 1199 。丹尼尔·克鲁格勒写道:

it could be useful to allow this form in a case where a single piece of code is used in multiple configurations, in some of which the function is constexpr and others deleted; having to update all declarations of the function to remove the constexpr specifier is unnecessarily onerous.

关于c++ - constexpr 和函数体 = 删除 : what's the purpose?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40031140/

相关文章:

c++ - 这个模板创建的实际源代码是什么样的?

c++ - 最快的 c++/STL 算法在成对的集合中查找字符串

c++ - const char * 值生命周期

c++ - gcc v10 和 v9 : bug or feature 之间的 constexpr 差异

c++ - 是否可以在 C++ 中加载文件的 constexpr 函数?

c++ - 我想接受用户输入并在我的 shell 环境中输出它

c++ - `vcall' {0,{flat}}' 在下面的示例中表示什么?

c++ - 如何使用 C++ 在 ostream 中插入缓冲区

c++ - 如何在C++中定义数组的数组

c++ - 如何从 constexpr char* 构建更大的 std::array