- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直试图理解 std::nullopt_t
的基本原理不允许是DefaultConstructible
在 C++17(它被引入的地方)及更高版本中,并在此过程中克服了一些编译器差异混淆。
考虑以下违反规范的(它是 DefaultConstructible
)实现 nullopt_t
:
struct nullopt_t {
explicit constexpr nullopt_t() = default;
};
这是 C++11 和 C++14(无用户提供的 ctor)中的聚合,但不是 C++17(
explicit
ctor)和 C++20(用户声明的 ctor)中的聚合.
struct S {
constexpr S() {}
S(S const&) {}
S& operator=(S const&) { return *this; } // #1
S& operator=(nullopt_t) { return *this; } // #2
};
int main() {
S s{};
s = {}; // GCC error: ambiguous overload for 'operator=' (#1 and #2)
}
这在 C++11 到 C++20 中被 GCC(各种版本,比如 v11.0)拒绝,但在 C++11 到 C++ 中被 Clang(比如 v12.0)和 MSVC(v19.28)接受C++20。
nullopt_t
(如上)是一个聚合,而它 nullopt_t
的复制列表初始化#2
处的复制赋值运算符所需的对象可行,最佳答案
为什么是 nullopt_t
必须是 DefaultConstructible
首先?nullopt_t
的规范要求不得为 DefaultConstructible
回想起来,可以说是一个基于标签类型的一些 LWG 和 CWG 混淆的错误,并且这种混淆的解决仅在 std::optional
之后才出现。是 brought in from the Library Fundamentals TS Components .
首先,nullopt_t
的当前 (C++17, C++20) 规范, [optional.nullopt]/2 , 需要 [ 重点矿]:
Type
nullopt_t
shall not have a default constructor or an initializer-list constructor, and shall not be an aggregate.
[...] In particular,
optional<T>
has a constructor withnullopt_t
as a single argument; this indicates that an optional object not containing a value shall be constructed.
variant
、
any
和
optional
的同构界面),其中一篇论文是介绍
std::optional
的一部分, 有一个关于
nullopt_t
的讨论, 一般的标签类型和
DefaultConstructible
需求 [
重点矿]:
No default constructible
While adapting
optional<T>
to the newin_place_t
type we foundthat we cannot anymore usein_place_t{}
. The authors don't considerthis a big limitation as the user can usein_place
instead. It needsto be noted that this is in line with the behavior ofnullopt_t
asnullopt_t{}
fails as no default constructible. Howevernullptr_t{}
seems to be well formed.Not assignable from
{}
After a deeper analysis we found also that the old
in_place_t
supportedin_place_t t = {};
. The authors don't consider this a big limitation as we don't expect that a lot of users could use this and the user can usein_place
instead.in_place_t t;
t = in_place;It needs to be noted that this is in line with the behavior of
nullopt_t
as the following compile fails.nullopt_t t = {}; // compile fails
However
nullptr_t
seems to be support it.nullptr_t t = {}; // compile pass
To re-enforce this design, there is an pending issue 2510-Tag types should not be
DefaultConstructible
Core issue 2510.
DefaultConstructible
[
重点矿]:
(LWG) 2510. Tag types should not be
DefaultConstructible
[...]
Previous resolution [SUPERSEDED]:
[...] Add a new paragraph after 20.2 [utility]/2 (following the header synopsis):
- -?- Type
piecewise_construct_t
shall not have a default constructor. It shall be a literal type. Constantpiecewise_construct
shall be initialized with an argument of literal type.
DefaultConstructible
的方式解决。 ,如
explicit
就足够了[
重点矿]:
(CWG) 1518. Explicit default constructors and copy-list-initialization
[...]
Additional note, October, 2015:
It has been suggested that the resolution of issue 1630 went too far in allowing use of explicit constructors for default initialization, and that default initialization should be considered to model copy initialization instead. The resolution of this issue would provide an opportunity to adjust that.
Proposed resolution (October, 2015):
Change 12.2.2.4 [over.match.ctor] paragraph 1 as follows:
[...] For direct-initialization
or default-initialization, the candidate functions are all the constructors of the class of the object being initialized. [...]
explicit
还暗示该类型不是聚合,这反过来是 LWG Core Issue 2510 的最终解决方案(基于 CWG Core Issue 1518 的最终解决方案)
(LWG) 2510. Tag types should not be
DefaultConstructible
[...]
Proposed resolution:
[...] In 20.2 [utility]/2, change the header synopsis:
// 20.3.5, pair piecewise construction
struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
constexpr piecewise_construct_t piecewise_construct{};[...]
std::optional
的提案中。 ,可以说是疏忽,我想声明
nullopt_t
不必是
DefaultConstructible
, 只是,与其他标签类型一样,它应该有一个用户声明的
explicit
构造函数,它禁止它作为空括号复制列表初始化的候选对象,因为它不是一个聚合,并且唯一的候选构造函数是
explicit
.
S& operator=(nullopt_t)
赋值运算符不适用于赋值
s = {};
, 作为空括号
{}
将需要聚合初始化或复制列表初始化来创建
nullopt_t
(临时)对象。
nullopt_t
,但是(通过惯用的标签实现:我上面的实现),根据
P0398R0 (解决了 CWG 核心问题 1518),既不是聚合,也不是其默认构造函数参与复制列表初始化(来自空括号)。
SUSPENDED
2015 年 6 月 15 日,在 CWG 核心问题 1630 的决议更改之前(“问题 1630 的决议走得太远了”)。现在根据来自此问答的 ping 重新打开票证。
关于c++ - 来自空括号的不明确复制分配的编译器差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67469039/
我的问题由两部分组成。 我注意到使用 cc 编译器的 sparc(sun) 上的 memalign(block_size,bytes) 不检查字节是否为 2 的幂,这与使用 mvsc 编译器的 int
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
当我尝试在我的 gwt-maven Projekt 上进行 maven-install 时,我得到了这个错误: [ERROR] Failed to execute goal org.apache.ma
gcc 有一个选项 -s 来生成汇编源代码。 csc(MS C# 编译器)或 dmcs(mono C# 编译器)是否等价?我的意思是那些编译器是否提供了一个选项来生成可以读取而不是执行二进制文件的 I
我在 matlab simulink 中有一个模型。我把matlab安装在D盘了。当我运行模型时,出现以下错误: Unable to locate a C-compiler required by S
我非常喜欢 Visual Studio 2012,因为 GUI 非常快速和灵活。问题是我需要 VS2010 的 VC++-Compiler。在 VS 2012 中设置旧的编译器、SDK 有什么可能吗?
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我正在为类似 C 的语言开发编译器,但在语义分析和代码生成阶段遇到了一些困难。我的问题如下:1) 对于 if 语句,语法如下: if (expression) then statement1; sta
我想了解 php 编译器/解释器的工作原理。 我试图下载 php 源代码并试图了解它是如何工作的。我找不到合适的文档。如果有人可以阐明制作 php 编译器的模块以及 apache 服务器如何使用 ph
我有一些关于 python 的问题 为什么没有 python 编译器来创建本地代码?我找到了 py2exe 等,但它们只是随附了一个 python 解释器,因此,它又是执行代码的解释器。 是否无法创建
本文将是JVM 性能优化系列的第二篇文章(第一篇:传送门),Java 编译器将是本文讨论的核心内容。 本文中,作者(Eva Andreasson)首先介绍了不同种类的编译器,并对客户端编译,服务器
在 *nix 之类的系统或适当的工具包下是否有任何用于 ActionScript 3 的编译器来处理 Flash? 最佳答案 Flex SDK编译器 — mxmlc — 还将编译普通的 ActionS
我正在做一个C项目。但是其他人告诉我,由于没有C++编译器,所以无法构建它。 我不知道如何禁用C++的检测。这该怎么做? 最佳答案 检测C和C++工具链是CMake的默认行为。要禁用此行为,您需要手动
我正在寻找可以嵌入到我的程序中的 JIT 编译器或小型编译器库。我打算用它来编译动态生成的执行复数运算的代码。生成的代码在结构上非常简单:没有循环,没有条件,但它们可能很长(由 GCC 编译时只有几
多年来,我一直在 VB.NET 中使用 DEBUG 编译器常量将消息写入控制台。我也一直在以类似的方式使用 System.Diagnostics.Debug.Write。我一直认为,当 RELEASE
我了解编译器的前端和后端结构。但是,我不确定为什么编译器经常分为前端和后端。我相信有很多原因,你能给我几个吗?因为,大多数书籍/网站会告诉您它们是什么,但无法告诉您原因! 谢谢你。 最佳答案 前端处理
我有很多 JS 文件。其中一些相互依赖。其中许多依赖于 jQuery。我需要一种工具,它可以接受一个文件作为参数,传递地获取其所有依赖项,并以正确的顺序将它们编译成一个文件(基于依赖项) 依赖信息并不
我正在阅读著名的紫龙书第二版,但无法从第 65 页获取有关创建第一组的示例: 我们有以下语法(终端以粗体显示): stmt → expr; | if ( expr ) stmt | for ( opt
我正在寻找将 C# 语法编译为 native 代码(或者可能编译为 C++?)的选项。我对拥有正式成为该语言一部分的所有库不感兴趣,只是能够像编写 C++ 程序一样编写程序,但使用语言结构,例如部分类
编译器(例如:gcc)中的 -march 标志真的很重要吗? 如果我使用 -march=my_architecture 而不是 -march=i686 编译所有程序和内核,会不会更快 最佳答案 是的,
我是一名优秀的程序员,十分优秀!