草庐IT

const_buffers

全部标签

c++ - 为什么 =default on operator= 在有 const 成员时编译?

classFoo{public:Foo&operator=(constFoo&)=default;private:constinti=0;};为什么=default在那里被允许?它编译没有错误。我认为=default应该失败,因为它不可能分配给const变量?到底发生了什么? 最佳答案 当无法生成该函数时(就是这种情况),=default会将其生成为=deleted。如果您尝试使用该赋值运算符,您的编译器应该produceanerror. 关于c++-为什么=defaultonopera

c++ - clang 3.3 和 GCC 4.7 const v 的 constexpr

我刚刚尝试在Ubuntu13.04上使用带有GCC4.7.3标准库头文件的clang3.3编译大量代码。这一切都很顺利,除了一个问题。这段代码已经在这台机器上用标准的Ubuntuclang3.2包编译,所以我假设这是clang3.3编译器的一些变化。与使用复杂header的const和constexpr有关的问题。特别是复杂类型具有以下代码块#ifdef__GXX_EXPERIMENTAL_CXX0X__//_GLIBCXX_RESOLVE_LIB_DEFECTS//DR387.std::complexover-encapsulated.constexprdoublereal(){re

c++ - 为什么一旦我将引用声明为 const,它就可以接受不同类型的数据?

你好,我想弄清楚这件事..假设我有这段代码。inta=5;double&b=a;//Error.然后,一旦我将第二行声明为const,编译器就不会再报错了。constdouble&b=a;//Correct.背后到底是怎么回事,为什么const解决了问题。 最佳答案 一个int需要先转换为double。该转换会产生一个临时纯右值,并且这些不能绑定(bind)到对非常量的引用。对const的引用将延长临时变量的生命周期,否则临时变量会在创建它的表达式末尾被销毁。{inta=0;floatf=a;//atemporaryfloatisc

c++ - operator const char* 以奇怪的方式覆盖(?)我的另一个变量

#include#includeclassVector{double_x;double_y;public:Vector(doublex,doubley):_x(x),_y(y){}doublegetX(){return_x;}doublegetY(){return_y;}operatorconstchar*(){std::ostringstreamos;os这个程序的输出:$./a.outVectorw1(1.1,2.2)Vectorw2(3.3,4.4)Vector(3.3,4.4)Vector(3.3,4.4)我不明白为什么会得到输出。似乎是“constchar*n2=w2;”覆盖

c++ - 当应用于原始类型时,通过引用调用 const 是否会提高性能?

关于对象(尤其是字符串),按引用调用比按值调用更快,因为函数调用不需要创建原始对象的拷贝。使用const,还可以确保引用不被滥用。我的问题是,如果使用bool、int或double等原始类型,const按引用调用是否也更快。voiddoSomething(conststring&strInput,unsignedintiMode);voiddoSomething(conststring&strInput,constunsignedint&iMode);我的怀疑是,一旦原始类型的字节大小超过地址值的大小,就使用按引用调用是有利的。即使差异很小,我也想利用,因为我经常调用其​​中一些函数。

c++ - Protocol Buffers (protobuf) v3.0.0-alpha-2 中的可选字段和约束

我目前正在使用Google的v3.0.0-alpha-2ProtocolBuffers.据我所知,v3删除了required关键字,extensions字段的关键字和默认值以简化原型(prototype)语言。我不理解的是proto3中optional关键字的含义。示例:syntax="proto3";packagefw.example;messageExampleMessage{optionalstringoptional_string=1;stringnormal_string=2;}问题:optional_string和normal_string有什么区别除了名称和标签?我已经阅

c++ - 将 const 添加到 size_t 会导致编译失败是标准行为吗?

我最近读了一篇很酷的文章:https://akrzemi1.wordpress.com/2015/08/20/can-you-see-the-bug/在ideone上玩简化版时,我得到了令人惊讶的行为:#include#includeusingnamespacestd;intmain(){constsize_tsz=258;strings{sz,'#'};assert(2==s.size());}不编译,但是删除const的相同程序编译:#include#includeusingnamespacestd;intmain(){size_tsz=258;strings{sz,'#'};as

c++ - 没有用于使用可变参数调用 std::forward(const std::string &) 的匹配函数

我正在尝试为不可复制、不可移动的类制作一个可移动的包装器,但是我在将conststd::string变量传递给构造函数时遇到问题。下面的最小示例会产生以下错误:#include#include#include#includestructX{std::stringx;X(conststd::string&x):x(x){}X(constX&x)=delete;X(X&&x)=delete;};structWrapper{std::unique_ptrx;Wrapper(constWrapper&wrapper)=delete;Wrapper(Wrapper&&wrapper)=defau

c++ - 将 const 添加到引用

我想通过typedefconstAB;将const添加到引用类型。不知何故它不起作用。这在C++中是不可能的吗?测试:#includetypedefint&A;typedefconstAB;//::typeB;//alsodoesn'twork.static_assert(std::is_const::type>::value,"isconst");intmain(){return0;}编译错误:add2.cpp:5:1:error:staticassertionfailed:isconststatic_assert(std::is_const 最佳答案

c++ - 如何在现代 C++ 中使用生成器初始化 const 容器?

为了避免可变的容器/状态,我目前想知道什么是最接近从某些输入构建constSTL容器的东西,例如constvectorinput={2,13,7,1};我想做的是这样的:constautotransformed=generate_from>(input.begin(),input.end(),to_string);do_something(transformed);虽然您发现最多的方法是创建一个可变对象并对其进行修改(我想避免这种情况):vectorbad_mutable_container;for(constauto&elem:input){bad_mutable_container