草庐IT

限定词

全部标签

c++ - 使用 ref 限定符实现方法

我无法实现以下代码templatestructFoo{std::vectorvec;std::vectorgetVector()&&{//fillvectorifempty//andsomeotherworkreturnstd::move(vec);}std::vectorgetVectorAndMore()&&{//dosomemorework//returngetVector();//notcompilereturnstd::move(*this).getVector();//seemswrongtome}};intmain(){Foofoo;autovec=std::move(f

c++ - Ref 限定的成员函数的目的是什么?

这个问题在这里已经有了答案:Whatis"rvaluereferencefor*this"?(3个回答)关闭9年前.阅读时http://en.cppreference.com/w/cpp/language/member_functions,我遇到了一些我以前从未见过的东西:lvalue/rvalueRef-qualifiedmemberfunctions。他们的目的是什么? 最佳答案 请阅读以下内容:Duringoverloadresolution,non-staticcv-qualifiedmemberfunctionofclas

c++ - 强制非限定名称成为依赖值

在一些遗留测试框架代码中存在一种模式,它依赖于VisualC++中断的两阶段查找,在移植到其他编译器时会导致头痛。我知道有许多解决方案可以解决这个问题,但都需要“广泛”的结构更改。虽然我相当肯定没有,但我很好奇是否可能有一个“简单”的hack可以在符合标准的编译器中获得所需的行为,并且只需要很少的必要更改。在此示例中可以看到该模式:#include//global"system"functiontotest;generallysomethinglike`fopen`inarealtestconstchar*GetString(){return"GLOBAL";}//providesno

c++ - "volatile"限定符和编译器重新排序

编译器无法消除或重新排序对volatile限定变量的读/写操作。但是如果存在其他变量(可能是也可能不是volatile-qualified)的情况呢?场景1volatileinta;volatileintb;a=1;b=2;a=3;b=4;编译器能否重新排序第一个和第二个,或者第三个和第四个赋值?场景2volatileinta;intb,c;b=1;a=1;c=b;a=3;同样的问题,编译器可以重新排序第一个和第二个,还是第三个和第四个赋值? 最佳答案 C++标准说(1.9/6):Theobservablebehaviorofthe

c++ - 对依赖基类成员的非限定访问导致 "A declaration of [x] must be available"

代码://test3.cpp#includeusingnamespacestd;templatestructptr_stack_tp;templatestructptr_stack_tp:publicstack{~ptr_stack_tp(){while(!empty()){operatordelete(top());pop();}}};intmain(){}错误信息(gcc4.7.2):test3.cpp:Indestructor'ptr_stack_tp::~ptr_stack_tp()':test3.cpp:15:23:error:therearenoargumentsto'em

c++ - 声明和定义之间 const 限定符的使用不一致

我注意到在函数声明中存在的值参数上可以有const限定符,然后在定义中省略。这不会改变函数的签名。它实际上编译得很好。我还注意到常规类和模板类之间的行为不同。在GCC和Clang中的处理方式也有所不同。考虑以下代码:templatestructA{voidf(constint);};templatevoidA::f(intx){x=0;}structB{voidf(constint);};voidB::f(intx){x=0;}voidf(){Aa;a.f(0);Bb;b.f(0);}当我使用GCC编译时,我没有收到任何错误。使用Clang我得到:test.cpp:10:7:error

c++ - const-reference 限定成员函数

引用限定成员函数的股票示例似乎是这样的:#include#include#include//Easyaccesstoliteralsusingnamespacestd::literals;//FilewrapperclassFile{private://ThewrappedfileFILE*_file;public:File(constchar*name):_file(fopen(name,"r")){//unabletoopenthefile?if(!_file)throwstd::runtime_error{"Unabletoopenfile:"s+name};}~File(){f

c++ 右值引用和 const 限定符

const限定的众多好处之一是使API更易于理解,例如:templateintfunction1(Tconst&in);//clearly,theinputwon’tchangethroughfunction1通过引入右值引用,可以从完美转发中受益,但通常会删除const限定符,例如:templateintfunction2(T&&in);//canexplicitlyforwardtheinputifit'sanrvalue除了文档,还有什么好的方法来描述function2不会改变它的输入吗? 最佳答案 templateintfu

c++ - 为什么在限定的依赖名称之前需要关键字 "typename",而不是在限定的独立名称之前?

classA{staticintiterator;classiterator{[...]};[...]};我(我想我)理解这里需要typename的原因:templatevoidfoo(){typenameT::iterator*iter;[...]}但我不明白这里不需要typename的原因:voidfoo(){A::iterator*iter;[...]}谁能解释一下?编辑:编译器之所以没有后者的问题,我发现在一个评论中得到了很好的回答:在A::iterator的情况下,我不明白为什么编译器不会将它与staticintiterator混淆?-xcrypt@xcrypt因为它知道两个

c++ - 从函数类型中剥离所有限定符

给定一个可能带有cv-qualifier-seq和ref-qualifier的可能varargs函数类型,是否可以编写一个类型特征来去除所有限定词不写4*3*2=24部分特化?templatestructstrip_function_qualifiers;templatestructstrip_function_qualifiers{usingtype=R(Args...);};templatestructstrip_function_qualifiers{usingtype=R(Args...,...);};templatestructstrip_function_qualifier