草庐IT

arm_const_structs

全部标签

c++ - 为什么我不能 push_back 到 const 元素的 vector ?

这个问题在这里已经有了答案:DoesC++11allowvector?(5个答案)关闭7年前。push_back按预期工作到非常量元素vector:std::vectorfoo;intbar=0;foo.push_back(bar);但为什么下面的不可能呢?std::vectorfoo;constintbar=0;foo.push_back(bar);更准确地说,为什么可以创建foo对象但不能对其调用push_back?

c++ - std::unique_ptr 转移 const 对象的所有权

我有一个A类的对象在方法内部创建。此方法还创建对象的实例B将对象A作为构造函数参数刚刚创建。B必须取得对象的所有权A但它不能修改它。这意味着AB时应删除被删除,但在B的生命周期内它不能修改A.在本例中为std::unique_ptr作为B的成员变量是转移A所有权的正确方法(在std::move的构造函数中使用B)并保证它不会被修改? 最佳答案 是的,这正是您正在寻找的语义。std::unique_ptr声明“我拥有T对象。”指向constA的指针(原始或智能)声明“我无法修改我指向的A”。总而言之,这正是您所追求的。

【交叉编译环境】安装arm-linux交叉编译环境到虚拟机教程(简洁版本)

就是看到了好些教程有些繁琐,我就写了一个我这个解压安装的交叉编译环境是LinaroGCC的一个版本,可以用于在x86_64的主机上编译arm-linux-gnueabihf的目标代码步骤来了在你的Ubuntu系统中创建一个目录,例如/usr/local/arm,然后将下载好的gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz复制到该目录下。在该目录中对交叉编译工具进行解压,使用命令sudotar-vxfgcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz。解压完成后

c++ - shared_ptr 中对 const int 的 undefined reference

我有一个配置类//config.hppclassConfig{public:staticconstexprinta=1;staticconstexprintb=1;}并包含在main.cpp中//main.cpp#include"config.hpp"intmain(){std::coutstream=std::make_shared(Config::a);//compileerror}编译器说未定义对Config::a的引用它在使用cout时有效,但在shared_ptr构造函数中时无效。我不知道为什么会这样。 最佳答案 请注意s

c++ - 声明为 consts 的函数 args 随定义变为非常量

这个问题在这里已经有了答案:Whydoesafunctiondeclarationwithaconstargumentallowcallingofafunctionwithanon-constargument?(4个答案)关闭4年前。代码classA{public:voidf(constinti);};voidA::f(inti){std::cout为什么编译器在这种情况下不报错?为什么定义会覆盖常量参数?此外,当参数的类型为reference(&)时,编译器会抛出错误,但在这种情况下为什么不呢?是否有任何编译器标志可以对这些提到的情况发出警告?我对编译器错误POV更感兴趣。因为可以很

c++ 何时返回 const char* 而不是 std :string

在下面的类中,speak()返回constchar*而不是std::string有什么好处或原因吗?classAnimal{protected:std::stringm_name;Animal(std::stringname):m_name(name){}public:std::stringgetName(){returnm_name;}constchar*speak(){return"???";}}; 最佳答案 std::string带有许多可能不需要和未使用的功能。如果您不想要所有这些功能,您应该考虑使用成本。传递std::st

c++ - 为什么要给小类成员返回一个 const 引用?

我觉得这个问题肯定在某个地方,但要么我找不到正确的搜索词,要么不知何故遗漏了。假设我有这样保护其成员的类......classMyClass{intm_value;public:MyClass(intv):m_value(v){}intvalue()const{returnm_value;}}我已经看到遍及SO的示例代码,而是像这样返回对成员变量的const引用......classMyClass{intm_value;public:MyClass(intv):m_value(v){}constint&value()const{returnm_value;}//^^^^^^^^^^}我

c++ - 不同类型的 `decltype((const int)a)` 和 `decltype((const int)1)`

//g++7.3templatestructtd;intmain(){inta=1;tdt1;tdt2;return0;}编译结果如下:错误:聚合‘tdt1’类型不完整,无法定义错误:聚合‘tdt2’类型不完整,无法定义那么,为什么decltype((constint)a)的类型是和decltype((constint)1)不一样? 最佳答案 说明符decltype((constint)a)和decltype((constint)1)都解析为int。这是因为没有const非类类型的纯右值,如C++17[expr]中所述:Ifaprv

c++ - struct S { int align; 之间的区别}; (在 struct 关键字之后命名)和 struct { int align; } S; (结构定义后的名称)

#includestructHeader{unsignedlonglongintalignment;};intmain(void){structHeaderheader;//note:wecanloosethe'struct'inC++structHeader*pheader=&header;return0;}上面的程序在C和C++中都能完美编译。但是当我将Header结构更改为:struct{unsignedlonglongintalignment;}Header;它失败并在C中显示以下消息:错误:“Header”的存储大小未知在C++中:error:aggregate‘main()

c++ - 为什么 const vector<const pair<...>> 给出 'cannot be overloaded' 错误?

我有这个简单的代码:#include#includevoidfoo(conststd::vector>&networks){for(autop:networks){}}voidbla(conststd::vector>&networks){for(autop:networks){}}这会在bla()中产生一个错误:mrvn@frosties:~%g++-O2-W-Wall-g-std=gnu++17-cbla.ccInfileincludedfrom/usr/include/x86_64-linux-gnu/c++/5/bits/c++allocator.h:33:0,from/usr