草庐IT

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

c++ - 有没有办法构建 C++ 自定义限定符?

有没有办法实现自定义类型限定符(类似于const)?我想只允许在具有相同资格的函数中调用具有正确资格的函数。假设我会:voidallowedFunction();voiddisallowedFunction();//Onlyallowedtocallallowedfunctions.voidfoo(){allowedFunction();disallowedFunction();//Causecompiletimeerror}//Isallowedtocallanyfunctionitwants.voidbar(){allowedFunction();disallowedFunctio

c++ - 如何强制调用 const 限定函数重载?

我试图在类中调用const函数,但存在同名的非const函数。注意:我不能只更改名称。classMyQuestion{voidfun(){cout 最佳答案 选项#1:通过指向const限定类型的指针调用该函数:voidcall(){static_cast(this)->fun();//~~~~^}c++11:voidcall(){constauto*that=this;//~~^that->fun();}c++17:voidcall(){std::as_const(*this).fun();//~~~~~~~^}选项#2:使调用函

c++ - result_of 用于具有 cv 限定参数的成员对象

鉴于以下声明:structMyClass{};typedefintMyClass::*Mp;在我尝试过的gcc6.2和Clang编译器上,result_of::type产量int&&.我的问题总结:为什么int&&而不是constint&&或者干脆int?更多背景:标准规定result_of是这样定义的:themembertypedeftypeshallnamethetypedecltype(INVOKE(declval(),declval()...));该标准还以这种方式为指向成员对象的指针定义了INVOKE:—t1.*fwhenN==1andfisapointertodatamem