草庐IT

const-reference

全部标签

c++ - 从 const 类继承

我想从一个带有const说明符的类继承,如下所示:classProperty{intget()const;voidset(inta);};classConstChild:publicconstProperty{//CannevercallA::set()withthisclass,evenif//theinstantiationofthisclassisnotconst};classNonConstChild:publicProperty{//CancallbothA::set()andA::get()dependingon//theconstnessofinstantiationof

c++ - 为什么 std::reference_wrapper 不是默认可构造的?

我觉得防止std::reference_wrapper默认构造使其更难使用,即使使用默认构造的reference_wrapper会导致运行时异常。然而,一个reference_wrapper是完全可复制的,因此它的值始终可以更改,那么为什么要阻止它默认具有the空引用?它使许多用例变得更加简单,并且有了它,建议的observer_ptr不再需要-为什么需要冗余?默认构造reference_wrapper会统治他们!想法? 最佳答案 However,areference_wrapperisperfectlycopyable,soit'

c++ - 具有 const 引用的可变参数模板特化

如何特化具有参数常量引用的可变参数模板函数?例子:templateTfoo(Args...args)=delete;templateintfoo(inta,constchar*str,constTest&t){....}//Failstocompile//templateintfoo(inta,constchar*str,Test){....}//Okintmain(){autoi=foo(10,"teststring!",t);return0;}当使用声明的constTest&参数调用函数foo时,编译器无法看到专门的函数并回退到已删除的函数:error:useofdeletedfu

c++ - 检查/修改迭代器 "constness"

我有两个半密切相关的问题。给定一个作为模板参数传递的STL迭代器类型:如何判断类型对应的是常量迭代器还是非常量迭代器?替代1.,如何强制(例如使用enable_if)此类型对应于非常量迭代器?如何从非常量迭代器获取迭代器的const-版本(反之亦然)?[注意:已在thispost中回答;毫不奇怪,你不能。]这个问题来自哪里:我写了一个小类来促进vector上的算术/关系/代数运算(vector我的意思是一维固定大小的数据,而不是STLvector)。我没有强加一个特定的数据容器,而是定义了一个接口(interface)并派生了几个可能的容器,这些容器基本上“包装”了各种存储数据的方式。

c++ - 对 `forkpty' 的 undefined reference

所以我在Ubuntu10.04的Eclipse中开发我的项目。我有以下代码行:#includepid_tpid;intmaster;pid=forkpty(&master,NULL,NULL,NULL);但是当我尝试在Eclipse中构建它时,出现错误:undefinedreferenceto'forkpty'知道如何解决这个问题吗? 最佳答案 您需要-lutil命令行参数(以使用libutil共享库)。对于eclipse:http://zetcode.com/articles/eclipsecdevelopment/选择项目属性。

c++ - 为什么我不能对 reference_wrapper<std::chrono::milliseconds> 的 vector 进行排序?

我想要一个std::vector的排序View但我不想修改原始容器。std::reference_wrapper看起来很适合这个,它对整数vector也适用。我创建了这个小例子:#include#include#include#include#includeintmain(){std::vectornumbers{1,42,3,9,5};std::vector>sorted_numbers(numbers.begin(),numbers.end());std::sort(sorted_numbers.begin(),sorted_numbers.end());std::coutdura

c++ - const_cast 是否会导致实际的代码排放?

const_cast真的只是告诉编译器“停止提示,将其视为非常量指针”的一种方式吗?有没有const_cast本身被翻译成实际机器代码的情况? 最佳答案 不,它只是在编译时删除了const属性。 关于c++-const_cast是否会导致实际的代码排放?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/759315/

c++ - const std::map<boost::tuples::tuple, std::string>?

//BOOSTIncludes#include//Boost::Assign#include//Boost::Assign::List_Of#include//Boost::Assign::Map_List_Of#include//Boost::Tuples//STDIncludes#include#include#include//Usingnamespacesusingnamespacestd;usingnamespaceboost;usingnamespaceboost::assign;//Constsconstmapquery_map=map_list_of("4556_SEL

c++ - const 引用临时异常

我们都知道像这样的事情在c++中是有效的:constT&x=T();同时:T&x=T();不是。在arecentquestion谈话导致这条规则。OP发布了一些明显让人联想到UB的代码。但我希望它的修改版本可以工作(这是修改版本):#includeusingnamespacestd;classA{public:A(intk){_k=k;};intget()const{return_k;};int_k;};classB{public:B(constA&a):_a(a){}voidb(){coutb();}这在一些机器上打印垃圾,在其他机器上打印10...对我来说听起来像UB:-)。但后来

c++ - 为什么返回 const Rational 而不是 Rational

看到operator*的实现如下:classRational{public:Rational(intnumerator=0,intdenominator=1);...private:intn,d;//numeratoranddenominatorfriendconstRationaloperator*(constRational&lhs,constRational&rhs){returnRational(lhs.n*rhs.n,lhs.d*rhs.d);}};这里有两个问题:Q1>为什么运营商*必须返回const有理而不是简单理性的Q2>定义友元函数时,是否应该关心访问修饰符?