在以下代码中:
#include <cstring>
template <unsigned len>
struct CharArray {
CharArray() {
memset(data_, 0, len);
}
char data_[len];
};
struct Foobar {
CharArray<5> a;
CharArray<3> b;
CharArray<0> c;
};
int main() {
Foobar f;
}
Run Code Online (Sandbox Code Playgroud)
该类型CharArray<0>最终将一个大小为零的数组作为其唯一成员。我知道这是一个 GCC 扩展和一般不安全的做法。问题不在于那个。
当我用 gcc 10.2.0 编译代码时,我收到以下警告:
<source>: In function 'int main()':
<source>:5:3: warning: array subscript 8 is outside array bounds of 'Foobar [1]' [-Warray-bounds]
5 | CharArray() {
| ^~~~~~~~~
<source>:18:10: note: while referencing 'f'
18 | Foobar f;
| ^
Run Code Online (Sandbox Code Playgroud)
使用 gcc9 …
我已经在 ubuntu 20.04 上安装了 gcc-10 编译器。我需要测试协程ts的工作,所以我找到了一个使用协程的例子并尝试编译它。
例子:
#include <coroutine>
#include <iostream>
struct simple {
static inline int x = 0;
int id = 0;
simple() : id{ x++ } { std::cout << id << " constructed\n"; }
simple(simple&&) : id{ x++ } { std::cout << id << " move constructed\n"; }
~simple() { std::cout << id << " destructed\n"; }
struct promise_type {
simple get_return_object() { return {}; }
void return_void() {}
void unhandled_exception() { std::terminate(); }
auto initial_suspend() noexcept …Run Code Online (Sandbox Code Playgroud) 使用 gcc 10.1 和 boost 1.73.0,以下代码
#include <boost/bimap.hpp>
int main() {
boost::bimap<int, int> lookup;
}
Run Code Online (Sandbox Code Playgroud)
使用 flags 编译失败-O2 --std=c++20,但使用 flags会成功-O2 -std=c++17(使用编译器资源管理器验证)。
这可能与以下问题有关:https : //github.com/boostorg/bimap/pull/15(已弃用std::allocator<void>)
我现在可以使用一些解决方法来使此代码成功编译--std=c++20吗?
我尝试使用 gcc 10 构建以下内容-std=gnu++20 -fconcepts:
template <std::signed_integral T>\nclass MyClass{ T a; };\n\ntemplate <std::unsigned_integral T>\nclass MyClass{ T a; };\nRun Code Online (Sandbox Code Playgroud)\n\n为什么这段代码会导致以下错误?
\n\n> declaration of template parameter \xe2\x80\x98class T\xe2\x80\x99 with different constraints\n> 55 | template <std::unsigned_integral T>\n> | ^~~\nRun Code Online (Sandbox Code Playgroud)\n\n应该没问题吧?
\ntemplate-specialization class-template c++-concepts c++20 gcc10