- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
标准在 dcl.constexpr/6 中说明了模板 constexpr 函数/构造函数:
If the instantiated template specialization of a constexpr function template or member function of a class template would fail to satisfy the requirements for a constexpr function or constexpr constructor, that specialization is still a constexpr function or constexpr constructor, even though a call to such a function cannot appear in a constant expression. If no specialization of the template would satisfy the requirements for a constexpr function or constexpr constructor when considered as a non-template function or constructor, the template is ill-formed, no diagnostic required.
有趣的部分是:
fail to satisfy the requirements for a ... constexpr constructor, that specialization is still a ... constexpr constructor
因此,即使构造函数被标记为 constexpr
,它也不能用于常量表达式。
为什么会有这条规则?当函数不满足要求时,为什么不删除 constexpr
?
当前的行为在两个方面是不好的:
constexpr
被悄悄地删除了。constexpr
构造函数),将是 dynamically initialized没有任何错误/警告(因为构造函数不是“真正的”constexpr)。这条规则是否有一些优点,可以平衡它的缺点?
最佳答案
此规则允许您编写模板化构造函数/函数并将其标记为 constexpr
,即使它并不总是 constexpr
(至少有时只是)。
例如,std::pair
有 constexpr
constructors , 但它当然可以在常量表达式之外使用。
这是非常明智的,因为否则你将不得不复制所有这些函数(一次使用 constexpr
,一次不使用),即使代码完全相同。让我们甚至不考虑歧义。
因为通常不可能证明一个模板永远不能满足 constexpr
,所以不需要对其进行诊断(但它的格式不正确,因此如果编译器可以证明这一点,则可以向您投诉给定情况)。
你是对的,如果你想指定“这个函数应该只在常量表达式中可用”,这不是很有用,但这不是这个措辞的目的。
编辑:澄清一下,constexpr
for functions 仅意味着“在常量表达式内求值是合法的”(更精确的措辞 here ), 不是 “只能在编译时求值”。相比之下,constexpr
变量必须使用常量表达式进行初始化。
另一个编辑:感谢@JackAidley,我们有确切的措辞要讨论!
If the instantiated template specialization of a constexpr function template would fail to satisfy the requirements for a constexpr function, the
constexpr
specifier is ignored and the specialization is not a constexpr function.
这个问题是“至少有一组参数可以对其函数进行常量计算”是“constexpr 函数要求”的一部分。因此,编译器无法实现此子句,因为不可能(通常)证明给定函数(或函数模板实例化)是否存在这样的集合。你要么必须进一步混淆这个要求,要么放弃这个方面。委员会似乎选择了后者。
关于c++ - constexpr构造函数不满足要求,但还是constexpr。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53632586/
我有一个类和构造函数,如下所示: def init(log, edge): if edge: return Helper(log, edge) return Booka
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
有人知道在 mongo 上安装标准 ubuntu 需要多少磁盘空间和内存吗?试图找出我的 VPS 需求... 最佳答案 没有最低要求,但我不建议在与您的网络服务器相同的机器上运行 Mongo。 Mon
我的 Django 项目有一个虚拟环境,但是当我 pip 击 pip freeze 时,我得到了一个必须是全局站 pip 包列表的东西,包括太多包,比如ubuntu包和这么多不相关的东西。无论 vir
我曾尝试在 Heroku 上部署我的应用程序,但 smth 出错了。 错误:找不到满足要求的版本 get==2019.4.13(来自 -r/tmp/build_53ad6d03_/requiremen
我无法将 semantic-ui-calendar npm 模块加载到我的应用程序中。 我已经使用脚本标签成功地将它加载到我的 HTML 中, 但每次我尝试将它加载到我的应用程序中时,我都会出错。 在
如何修复 php.ini 中“require”函数内的地址?它进行故障排除并显示错误: 警告:require (..) 无法打开流:没有这样的文件或目录。 文件“db_connection.php”工
我有一个在 Node.js 应用程序中使用的外部库 ( Objection.js )。我创建了一个基本模型类,它为我的实体模型扩展了 Objection 的 Model 类: const { Mode
有谁知道在哪里可以找到RHEL5的GLIBC2.7,如果没有这个,Android模拟器将无法启动。它会给出一条消息,要求GLIBC 2.7或更高版本。 我尝试在网上搜索,但没有找到 最佳答案 我也遇到
Android 设备是否有任何要求/指南?例如按钮数量或所需的最少按钮数量。 还有没有菜单和后退按钮的安卓设备吗? (我知道就可用性而言,没有菜单/后退按钮会杀死大多数应用程序,我只是想了解更多有关该
我想要求/包含一个文件并将其内容检索到一个变量中。 test.php index.php ".$test; ?> 类似于 file_get_contents() 但它仍应执行 PHP 代码。这可能吗
我想要求/包含一个文件并将其内容检索到一个变量中。 test.php index.php ".$test; ?> 类似于 file_get_contents() 但它仍应执行 PHP 代码。这可能吗
我正在尝试在我的 Linux Mint 发行版上安装一个 python 模块“pyAudioProcessing”(https://github.com/jsingh811/pyAudioProces
我已经创建了我的第一个 composer 包,它具有 MySQL 和 MongoDB 的功能,但是,它不需要两者。我意识到有人可能只想将这个包与两个数据库之一一起使用,目前我有: "require":
我想调试以下函数,但假设在调试器中查看 moreajaj 的参数等于什么(假设不像在这个人为的示例中那么明显)是有用的。我可以在调试器框架中打印它,但是在每个参数的每个框架中都这样做很烦人。在宣布每一
我有一些生成的 GNUmakefiles,我需要从中提取变量的值。 有没有一种简单的方法可以在不修改 makefile 的情况下查看变量的值? 仅供引用,变量包含 emacs c-macro-expa
我正在使用 aspell 在 Linux 上拼写检查 LaTeX 文档。我的文档经常包含各种编程语言的代码示例,我希望 aspell 在拼写检查时简单地跳过这些行。 我可以在文档中写些什么来关闭一段文
我有一个包含多个列的数据集... 一列是具有重复值的主石斑鱼列,另一列是具有 bool 值 (1,0) 的 NUMBER,如下所示: grp bool --- ---- A 1 A 1 A
出于测试目的,我正在尝试删除一些 amd 模块并从服务器重新加载更新版本 - 目的是不刷新浏览器。 我目前正在执行以下操作,但浏览器仍然没有从网络重新加载项目。 var scripts = docum
当我键入irb> require 'rubygems'时,它返回false。我的Rails应用程序中有很多 gem ,这些 gem 显然可以正常工作-耙子,activerecord等。这里可能出什么问
我是一名优秀的程序员,十分优秀!