问题标签 [c++20]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
373 浏览

c++ - 什么是事务性内存以及它在 C++ 编程中会发生什么变化 (C++20)

C++20 的一项提议特性是事务性内存。

它是什么?它会简化 C++ 中的多线程编程吗?

0 投票
1 回答
1206 浏览

c++ - 什么是 C++ 模块权限?

我在 C++ 模块的上下文中看到了一些对“权限”一词的引用,例如在https://gcc.gnu.org/wiki/cxx-modules中:

C++ 模块权限到底是什么?

0 投票
1 回答
1153 浏览

c++ - <=> 在 c++20 之前的代码中的合法出现

在 wandbox 中四处乱逛,我发现如果 clang 看到<=>出现在 C++17 或更早版本中,它实际上会发出警告。

我试图弄清楚如何<=>在 C++17 中编写一个合法的字符序列用例,但我想出的东西都觉得很做作。最可能的示例(imo)涉及使用模板:

活生生的例子

其他一切仍然涉及通过 name 明确提及比较函数operator<=。是否有更常见的外观<=>,我无法想象这会促使 clang 开发人员添加此警告?

0 投票
1 回答
2449 浏览

c++ - Why is std::variant unable to hold array object types while union can?

Here is simple example, We can define a low level union like this :

but we cannot declare std::variant like this(please do not care about syntax,correct me if I am wrong!, just grab the idea)

cppReference stated clearly that,

Template parameters

Types - the types that may be stored in this variant. All types must be (possibly cv-qualified) non-array object type

also,MSVC-v141(C++17) compiler has given a compilation error:

Error C2338 variant requires all of the Ts to be non-array object types ([variant.variant]/2).


std::variant is primarily a class template hence, Problem is it unable to deduce array type storage, since it requires only data layout/representation ?

0 投票
1 回答
512 浏览

c++ - 模板推导与隐式用户定义转换运算符

我试图实现一个涉及模板的用户定义类型转换的小例子。

我收到以下编译错误(来自 GCC 7.3.0):

main.cpp: 在函数'void changeScale(uint32_t&, Number)'中:

main.cpp:40:15:错误:'operator*=' 不匹配(操作数类型为'uint32_t {aka unsigned int}'和'Number')

幅度 *= 规模;

请注意注释掉的行 - 这行有效:

为什么不能自动推导出模板化的转换算子?提前感谢您的帮助。

[编辑]

我按照删除概念的建议来使用 Clang 并查看它的错误消息。我得到以下信息(这被截断但足够了):

因此,打开这些概念后,我假设强制转换 Number 的唯一方法是将其转换为无符号整数类型 - 那么为什么编译器不足以推断转换呢?

0 投票
1 回答
8694 浏览

c++ - 为什么我们需要 C++ 中的 spaceship <=> 运算符?

为什么我们在 C++ 中需要这样的运算符,它在现代 C++ 编程中有何用处?任何可以应用的真实世界代码示例都会有所帮助。

这个问题旨在了解现实世界中的实际应用,而无需阅读 Herb Sutter 的冗长建议。不过,没有冒犯该提案。

0 投票
2 回答
1011 浏览

c++ - 为什么我们使用概念和约束

我真的不明白为什么 C++20 会提供这样的功能。我需要有人指出如何优雅地使用此功能。这是一个例子:

现在我已经定义了一个概念。然后我将像这样约束一个函数模板:(好吧,我们将其命名为comp,实际上它就像std::min

所以问题是如果你这样称呼

发生编译错误

但是如果我们不使用约束,CE 也会发生。

所以这让我很困惑,他们都有CE,那我为什么要使用约束呢?

我想如果我想清理错误信息,我可以使用 SFINAE。

0 投票
3 回答
1237 浏览

c++ - 为什么 std::rel_ops::operators 在 C++20 中会被弃用?

根据cppreference.comstd::rel_ops::operator!=,>,<=,>=将在 C++20 中弃用。

背后的原理是什么?

0 投票
2 回答
1137 浏览

c++ - Why does std::span overload the function call operator for indexing?

Edit: this overload has been removed from the standard now, it seems.

From cppreference:

Returns a reference to the idx-th element of the sequence. The behavior is undefined if idx is out of range (i.e., if it is less than zero or greater than or equal to size()).

It makes sense to overload operator[] for indexing, as a span represents an object that can refer to a contiguous sequence of objects, but why is operator(), the function call operator, also overloaded for the same purpose? I don't believe there's anything similar to this in the standard library.

0 投票
1 回答
251 浏览

c++ - c++ 泛型 lambdas:模式类型推导

在 C++20 中,根据提案Familiar template syntax for generic lambdas,以下代码正确推导出类型 T:

是否可以在 C++17(或 14)中进行这种模式推导?

注意:我专门询问 lambda 表达式。