标签: gcc6

存在使用命名空间时的全局范围解析

请考虑以下代码:

namespace foo {
  namespace bar {
    class foo {};
  }
  class baz {};
}
using namespace foo::bar;

::foo::baz mybaz;
Run Code Online (Sandbox Code Playgroud)

这段代码有效吗?还是::foo含糊不清?或者::foo指的class foo是,没有::foo::baz.

谈到编译器,gcc 6.1.1似乎认为后者:

scope.cpp:9:8: error: ‘baz’ in ‘class foo::bar::foo’ does not name a type
 ::foo::baz mybaz;
        ^~~
Run Code Online (Sandbox Code Playgroud)

另一方面gcc 5.3.1,clang 3.8.0并且intel编译器16.0.3不会产生任何警告或错误.

我怀疑在C++ 14标准的3.4.3.2.2下,这应该是有效的而不是含糊不清的,但我不太确定.

编辑:此外,foo::baz mybaz只有clang报告一个模棱两可的错误.

c++ language-lawyer c++14 gcc6

13
推荐指数
1
解决办法
295
查看次数

为什么GCC似乎没有文件系统标准库?

我面临文件系统库的问题,它应该包含在c ++ 17编译器中,2天后我试图在raspberry pi中安装gcc-7.0.2但是它没有用,它无法识别命令gcc-7或g ++ - 7甚至-std=c++17所以我必须安装g ++ - 6和gcc-6 apt-get install ,在安装6版之后,编译器包含c ++ 17.我使用代码块作为IDE,我不得不添加一个新的编译器并添加选项-std = c ++ 17来启用它,但在主代码中,当我包含文件系统库时,它没有说这样的文件或目录.

我的问题是,如何才能正确添加c ++ 17编译器及其库(如文件系统)?

c++ gcc raspberry-pi gcc6 c++17

11
推荐指数
2
解决办法
9265
查看次数

为什么std :: equal比两个小std :: array的手动循环慢得多?

我正在分析一小段代码,这是一个更大的模拟的一部分,令我惊讶的是,STL函数等于(std :: equal)比一个简单的for循环慢得多,比较两个数组元素.我写了一个小测试用例,我认为这是两者之间的公平比较,而差异,使用Debian档案中的g ++ 6.1.1并不是无关紧要的.我正在比较有符号整数的两个四元素数组.我测试了std :: equal,operator ==和一个小的for循环.我没有使用std :: chrono来获得精确的时间,但是可以通过时间明确地看出差异./a.out.

我的问题是,给定下面的示例代码,为什么operator ==和重载函数std :: equal(调用operator ==我相信)需要大约40秒来完成,而手写循环只需要8s?我正在使用最新的基于英特尔的笔记本电脑.for循环在所有优化级别上都更快,-O1,-O2,-O3和-Ofast.我编译了代码 g++ -std=c++14 -Ofast -march=native -mtune=native

运行代码

循环运行了很多次,只是为了使肉眼看清楚.模运算符表示对其中一个数组元素的廉价操作,并用于防止编译器优化循环.

#include<iostream>
#include<algorithm>
#include<array>

using namespace std;
using T = array<int32_t, 4>;

bool 
are_equal_manual(const T& L, const T& R)
noexcept {
    bool test{ true };
    for(uint32_t i{0}; i < 4; ++i) { test = test && (L[i] == R[i]); }
    return test;
}

bool
are_equal_alg(const T& L, const T& R)
noexcept {
    bool test{ equal(cbegin(L),cend(L),cbegin(R)) };
    return test;
} …
Run Code Online (Sandbox Code Playgroud)

c++ performance stl c++14 gcc6

9
推荐指数
1
解决办法
404
查看次数

Valgrind报告不匹配f​​ree()/ delete/delete []

我正在编写一个可以在多个系统上运行的库(其中一些没有malloc或者是stdlib).在我的stdlib(不同的lib)中,我重写了newdelete运算符来对函数进行泛型调用(这个例子没有这些函数).每个系统都会覆盖对各自内存分配设备的这些通用调用.

问题是当我尝试这样做时.以下是一些重现问题的精简示例代码:

#include <cstdlib>

void* operator new(unsigned long size) {
        return std::malloc(size); // would normally call an intermediate function which would be overridden by the system
}

void operator delete(void* object) {
        std::free(object); // would normally call an intermediate function which would be overridden by the system
}
void operator delete(void* object, unsigned long size) {
        std::free(object); // would normally call an intermediate function which would be overridden by the system
}

class MyClass …
Run Code Online (Sandbox Code Playgroud)

c++ gcc valgrind new-operator gcc6

9
推荐指数
1
解决办法
1870
查看次数

为什么GCC 6假设数据是16字节对齐的?

(提前抱歉没有设法将我的问题减少到简单的失败测试用例...)

我遇到了升级到GCC 6.3.0以构建我们的代码库(相关标志:)的问题-O3 -m32.

具体来说,由于GCC优化,我的应用程序会在struct ctor调用中进行段错误.

在这个ctor中,GCC使用了movaps:

movaps %xmm0,0x30a0(%ebx)
Run Code Online (Sandbox Code Playgroud)

movaps 要求操作数为16字节对齐.但是在这个时间%ebx点,指向我的对象,不一定是16字节对齐.来自glibc:

"在GNU系统中,malloc或realloc返回的块的地址总是八的倍数(或64位系统上的十六个)."

因此segfault(建立时-O3 -m32).

为什么GCC假设分配的对象是16字节对齐?我误会了什么吗?

笔记:

  • 此结构上没有对齐提示或属性
  • 对象已通过默认new运算符初始化
  • 取决于优化水平:
    • 通过: -m32 -O2
    • 失败: -m32 -O2 -ftree-slp-vectorize
    • 通过: -m32 -O3 -fno-tree-slp-vectorize
    • 失败: -m32 -O3

这个其他项目似乎遇到了类似的问题:https://github.com/godotengine/godot/issues/4623

他们的调查指向-fvect-cost-model=dynamic.调查我的代码库而不是指向-ftree-slp-vectorize.

c++ gcc glibc memory-alignment gcc6

9
推荐指数
1
解决办法
858
查看次数

在gcc 6中实现std :: unique_ptr <T []> :: reset

从C++中的GCC 6开始,unique_ptr<T[]>::reset方法的声明/定义(不是仅接受的方法nullptr_t)如下所示:

template <typename _Up,
            typename = _Require<
              __or_<is_same<_Up, pointer>,
                    __and_<is_same<pointer, element_type*>,
                           is_pointer<_Up>,
                           is_convertible<
                             typename remove_pointer<_Up>::type(*)[],
                             element_type(*)[]
                           >
                    >
              >
           >>
  void
  reset(_Up __p) noexcept
  {
using std::swap;
swap(std::get<0>(_M_t), __p);
if (__p != nullptr)
  get_deleter()(__p);
  }
Run Code Online (Sandbox Code Playgroud)

在某些时候改变了这一点以实现N4089.根据该文件:

此函数的行为与主模板的重置成员相同,除非它不参与重载解析

- U是相同的类型pointer,或者

- pointer是类型element_type*,U是指针类型V*,V(*)[]可以转换为element_type(*)[].

让我们考虑以下示例:

std::unique_ptr<const char []> ptr1;
std::unique_ptr<char []> ptr2(new char[5]);
ptr1 = std::move(ptr2);
Run Code Online (Sandbox Code Playgroud)

由于版本6 GCC产生错误,抱怨它无法std::swap使用const char*& …

c++ gcc unique-ptr c++11 gcc6

8
推荐指数
1
解决办法
405
查看次数

是否存在或将存在target_clones属性的"全局"版本?

我最近玩过target_clonesgcc 6.1及以后的属性.它非常漂亮,但是,现在,它需要一种有点笨拙的方法; 想要多版本化的每个函数都必须手动声明属性.这不是最佳的,因为:

  • 它将编译器特定的东西放在代码中.
  • 它要求开发人员确定哪些功能应该接受这种处理.

让我们举一个例子,我想编译一些可以利用AVX2指令的代码.-fopt-info-vect将告诉我哪些函数是矢量化的,如果我构建-mavx2,所以编译器已经知道这一点.有没有办法在全局范围内告诉编译器:"如果你找到一个你觉得可以用AVX2优化的功能,那就制作多个版本,包括和不带AVX2的那个功能."?如果没有,我们可以拥有一个吗?

gcc avx avx2 gcc6

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

一组概念指针

我试图弄清楚我是否可以将概念用作类的接口,而不需要虚拟表的开销.我把一个实例组合在一起,但是我必须将我的类实例存储在由它们的公共继承而不是它们的共同概念定义的数组中.我没有看到关于概念数组的帖子中讨论的任何内容,但g ++ 6.3.0似乎不允许它.错误是:

$ g++ -fconcepts -std=c++1z custom_concept.cpp 
custom_concept.cpp: In function ‘int main()’:
custom_concept.cpp:37:20: error: ‘shapes’ declared as array of ‘IShape*’
    IShape* shapes[2] = {&square, &rect};  // doesn't work 
                    ^
custom_concept.cpp:39:25: error: ‘shapes’ was not declared in this scope
    for (IShape* shape : shapes ) 
                         ^~~~~~
Run Code Online (Sandbox Code Playgroud)

如果我将IShape*数组更改为Rectangle*数组(如导致第一个错误的数组下面的注释行),程序将按预期编译并运行.

为什么不允许使用概念指针数组?这可能会在未来的c ++版本中被允许吗?

(我的例子包括虚函数和继承,尽管我的目标是消除它们.我只是为了方便Rectangle*版本才能使用它们.如果我能让IShape*版本工作,我打算删除虚函数和遗产.)

这是代码:

#include <iostream>

template <typename T>
concept bool IShape = requires (T x, T z, int y)
{
    { T() } ; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays c++-concepts gcc6 c++20

6
推荐指数
1
解决办法
248
查看次数

gcc 6是否支持使用std :: sample(c ++ 17)?

我正在尝试使用以下命令来编译包含使用gcc版本6.3.0的这段c ++ 17代码:std::sampleg++ -std=gnu++17 -c main.cpp

但是我明白了:error: ‘sample’ is not a member of ‘std’...

#include <vector>
#include <algorithm>
#include <random>

int main()
{
    std::vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> b(5);

    std::sample(a.begin(), a.end(), 
                b.begin(), b.size(),
                std::mt19937{std::random_device{}()});

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

gcc 6是否支持使用 std::sample?(使用gcc 8.2.0可以正常编译)

我在这两页上找不到答案:

c++ gcc gcc6 c++17

6
推荐指数
1
解决办法
1940
查看次数

如何告诉gcc在struct中禁用填充?

我不确定它是否正常或者它是编译器错误但是我有一个包含很多成员的C结构.其中包括:

struct list {
    ...  
    ...
    const unsigned char nop=0x90; // 27 bytes since the begining of the structure
    const unsigned char jump=0xeb; // 28 bytes since the begining of the structure
    const unsigned char hlt=0xf4; // 29 bytes since the begining of the structure
    unsigned __int128 i=0xeb90eb90eb90eb90f4f4 // should start at the 30th byte, but get aligned on a 16 byte boundary and starts on the 32th byte instead
    const unsigned char data=0x66; // should start at the 46th …
Run Code Online (Sandbox Code Playgroud)

c gcc x86-64 padding gcc6

5
推荐指数
2
解决办法
2万
查看次数