编译以下代码
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给大会
main:
xorl %eax, %eax
ret
Run Code Online (Sandbox Code Playgroud)
https://gcc.godbolt.org/z/oQvRDd
如果现在iostream包括在内
#include <iostream>
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序集已创建.
main:
xorl %eax, %eax
ret
_GLOBAL__sub_I_main:
subq $8, %rsp
movl $_ZStL8__ioinit, %edi
call std::ios_base::Init::Init() [complete object constructor]
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
addq $8, %rsp
jmp __cxa_atexit
Run Code Online (Sandbox Code Playgroud)
打开完全优化(-O3). https://gcc.godbolt.org/z/EtrEX8
有人可以解释一下,为什么包含一个未使用的头更改二进制 什么是_GLOBAL__sub_I_main:?
我有以下代码片段,它接受std::vector<int> list并在所有向量元素中写入零.这个例子工作得非常好.
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<int> list {1, 1, 2};
auto reset = [](int & element){element = 0;};
auto print = [](int element) {std::cout << element << " ";};
std::for_each(list.begin(), list.end(), reset);
std::for_each(list.begin(), list.end(), print);
}
Run Code Online (Sandbox Code Playgroud)
如果我把改变来自载体的种类int来bool,代码将无法编译.
#include <vector>
#include <iostream>
#include <algorithm>
int main () {
std::vector<bool> list {true, true, false};
auto reset = [](bool & element){element = false;};
auto print = [](int element) {std::cout << …Run Code Online (Sandbox Code Playgroud) 自从std::any引入C++ 17以来.现在可以编写这样的代码了
#include <iostream>
#include <any>
#include <string>
int main () {
const double d = 1.2;
std::any var = d;
const std::string str = "Hello World";
var = str;
}
Run Code Online (Sandbox Code Playgroud)
双被分配给变量var和比std::string被分配给它.
为什么要std::any介绍?
我认为这违反了least astonishment rule,因为我发现很难想到一种情况,这可以用来表达更清楚,我想表达的内容.
有人能给我一个很好的例子吗std::any?
我喜欢为a中的内部向量保留内存,std::vector<std::vector<TYPE>>以避免在后续期间进行大量单个内存分配push_back.我完全不知道innerSize矢量,但我可以给出一个很好的估计.
std::resize 可以用作
vecs.resize(outerSize, std::vector<TYPE>(innerSize));
Run Code Online (Sandbox Code Playgroud)
在哪里outerSize和innerSize给予整数.这对我不起作用,因为默认构造函数不适用.但是std::reserve不提供这样的界面.
这是为所有内部向量保留记忆的好方法吗?
vecs.resize(outerSize);
for (auto &elem : vecs) {
elem.reserve(innerSize);
}
Run Code Online (Sandbox Code Playgroud) 结构化绑定已经与c ++ 17一起引入.它们能够声明从元组或结构初始化的多个变量.
此代码使用c++17编译器进行编译.
#include <iostream>
#include <tuple>
int main() {
auto tuple = std::make_tuple(1.0, 1);
auto [ d, i ] = tuple;
std::cout << "d=" << d << " i=" << i << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我没有声明变量auto我得到错误
错误:lambda表达式的预期体 [d2,i2] =元组;
#include <iostream>
#include <tuple>
int main() {
auto tuple = std::make_tuple(1.0, 2);
double d2;
int i2;
[d2 , i2] = tuple;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用clang version 4.0.0和编译选项-std=c++1z.
我可以将现有变量分配给结构化绑定吗?我需要使用auto …
我有以下包装类:
template <typename T>
class Remap {
public:
Remap(T *data, int *remap) : data(data), remap(remap){};
T &operator[](std::size_t idx) const { return data[remap[idx]]; }
private:
T *data;
int *remap;
};
Run Code Online (Sandbox Code Playgroud)
如果我称它为:
Remap<double> remap(data, remap);
Run Code Online (Sandbox Code Playgroud)
数据的类型double *.如果我试图让编译器(intel icc 15.0.3,-std = c ++ 11)推导出模板类型:
Remap remap(data, remap);
Run Code Online (Sandbox Code Playgroud)
它失败并显示错误消息:
argument list for class template "Remap" is missing
我尽量不违反DRY原则,因此想解决这个问题.
我使用的是intel c ++编译器icc版本18.0.3.
如果我编译下面的代码与-w3
#include <vector>
int main() {
std::vector<int> vec;
vec.push_back(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test_w3.cpp(6):备注#383:复制到临时值,引用临时使用的vec.push_back(2);
更换的2与一个常量变量作为
#include <vector>
int main() {
std::vector<int> vec;
const int a = 2;
vec.push_back(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有发出警告.
这个警告意味着什么?可以安全地忽略它(尽管需要无警告代码)吗?
std::string &func(int vlu)
{
std::string str;
str = std::to_string(vlu) + "something";
return str;
}
Run Code Online (Sandbox Code Playgroud)
上述功能明显不安全.
以下是另一个版本.
std::string &func(int vlu)
{
return std::to_string(vlu) + "something";
}
Run Code Online (Sandbox Code Playgroud)
我有一些问题:
第二个版本的编译器(gcc)没有给我任何警告.安全吗?我只是认为编译器(或其他什么?)将创建一个临时变量来保存表达式的返回std::to_string(vlu) + "something".所以第二个版本也不安全.我说对了?
c++ ×10
vector ×4
c++11 ×3
boolean ×2
c++17 ×2
assembly ×1
auto ×1
benchmarking ×1
c++14 ×1
contiguous ×1
continuous ×1
foreach ×1
gcc ×1
integration ×1
iostream ×1
jenkins ×1
memory ×1
reference ×1
stdany ×1
templates ×1