标签: gcc10

当 struct 将零长度数组作为唯一成员时,数组下标越界

在以下代码中:

#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 …

c++ gcc10

7
推荐指数
1
解决办法
173
查看次数

为什么 gcc-10 没有看到与协程 ts 相关的关键字?

我已经在 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)

c++ gcc c++20 c++-coroutine gcc10

6
推荐指数
0
解决办法
639
查看次数

Boost bimap 无法使用 gcc 10、c++20 进行编译。寻找临时修复

使用 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吗?

c++ boost c++20 gcc10

4
推荐指数
1
解决办法
486
查看次数

为什么概念类模板特化会导致错误

我尝试使用 gcc 10 构建以下内容-std=gnu++20 -fconcepts

\n\n
template <std::signed_integral T>\nclass MyClass{ T a; };\n\ntemplate <std::unsigned_integral T>\nclass MyClass{ T a; };\n
Run 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>       |           ^~~\n
Run Code Online (Sandbox Code Playgroud)\n\n

应该没问题吧?

\n

template-specialization class-template c++-concepts c++20 gcc10

2
推荐指数
1
解决办法
314
查看次数