请考虑以下代码:
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 ++ 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编译器及其库(如文件系统)?
我正在分析一小段代码,这是一个更大的模拟的一部分,令我惊讶的是,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) 我正在编写一个可以在多个系统上运行的库(其中一些没有malloc或者是stdlib).在我的stdlib(不同的lib)中,我重写了new和delete运算符来对函数进行泛型调用(这个例子没有这些函数).每个系统都会覆盖对各自内存分配设备的这些通用调用.
问题是当我尝试这样做时.以下是一些重现问题的精简示例代码:
#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) (提前抱歉没有设法将我的问题减少到简单的失败测试用例...)
我遇到了升级到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 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*& …
我最近玩过target_clonesgcc 6.1及以后的属性.它非常漂亮,但是,现在,它需要一种有点笨拙的方法; 想要多版本化的每个函数都必须手动声明属性.这不是最佳的,因为:
让我们举一个例子,我想编译一些可以利用AVX2指令的代码.-fopt-info-vect将告诉我哪些函数是矢量化的,如果我构建-mavx2,所以编译器已经知道这一点.有没有办法在全局范围内告诉编译器:"如果你找到一个你觉得可以用AVX2优化的功能,那就制作多个版本,包括和不带AVX2的那个功能."?如果没有,我们可以拥有一个吗?
我试图弄清楚我是否可以将概念用作类的接口,而不需要虚拟表的开销.我把一个实例组合在一起,但是我必须将我的类实例存储在由它们的公共继承而不是它们的共同概念定义的数组中.我没有看到关于概念数组的帖子中讨论的任何内容,但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) 我正在尝试使用以下命令来编译包含使用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结构.其中包括:
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) gcc6 ×10
c++ ×8
gcc ×7
c++14 ×2
c++17 ×2
arrays ×1
avx ×1
avx2 ×1
c ×1
c++-concepts ×1
c++11 ×1
c++20 ×1
glibc ×1
new-operator ×1
padding ×1
performance ×1
raspberry-pi ×1
stl ×1
unique-ptr ×1
valgrind ×1
x86-64 ×1