草庐IT

const_cast-ing

全部标签

c++ - Visual C++ ~ 不内联简单的 const 函数指针调用

亲爱的StackOverflowers,我得到了一段在MicrosoftVisualStudioC++2012上编译的简单代码:intadd(intx,inty){returnx+y;}typedefint(*func_t)(int,int);classA{public:conststaticfunc_tFP;};constfunc_tA::FP=&add;intmain(){intx=3;inty=2;intz=A::FP(x,y);return0;}编译器生成如下代码:intmain(){000000013FBA2430subrsp,28hintx=3;inty=2;intz=A:

c++ - 接受所有版本的 const/volatile 限定和 & vs && 的类模板特化

我专攻std::common_type适合我的类型。我定义了以下专业:common_type一切都很好。然后有人过来调用std::common_type.如果您传递引用与不传递引用(因为它在类型上调用std::decay),则默认版本的行为相同。但是,它并不遵循std::common_type的非引用版本,我需要它才能正常工作。有没有比必须做这样的事情更好的方法(为简单起见,省略对const的右值引用):common_typecommon_typecommon_typecommon_typecommon_typecommon_typecommon_typecommon_typecomm

c++ - 关于 const decltype(x)& 的问题

考虑以下代码:inta=1;constint&b=a;std::cout();它在clang3.5上编译,而GCC4.9给出以下错误:错误:“const”限定符不能应用于“constint&”根据标准,哪个是正确的?我的猜测是GCC符合标准,就像您不能执行int&constb=a;一样。 最佳答案 我相信代码是有效的并且两种类型是相同的。[dcl.ref]/1说:Cv-qualifiedreferencesareill-formedexceptwhenthecv-qualifiersareintroducedthroughtheus

c++ - volatile 和 const volatile std::tuple 和 std::get

查看C++11标准。我可以看到std::tuple_size的特化和std::tuple_element为volatile提供和constvolatile元组。templateclasstuple_element;templateclasstuple_element;templateclasstuple_size;templateclasstuple_size;但是std::get不提供特化volatile或constvolatile元组。我在GCC.4.8.1上尝试了以下代码volatilestd::tuplea(1,1);std::cout="(a)我收到错误:nomatching

c++ - 当编译器看不到可以完成转换的可能类型时,是否允许编译器优化 volatile 指针的 dynamic_cast?

看看这个小片段:structA{virtual~A(){}};structB{};boolfn(){A*volatilea=newA;returndynamic_cast(a);}是否允许编译器完全删除dynamic_cast,并将dynamic_cast转换为简单的nullptr;?这个问题的原因是这个answer.注意事项:假定volatile意味着编译器不能假定任何有关a的信息,因为它是易变的。这是一个question为什么。dynamic_cast可能不允许被删除的事实是程序中某处可能有一个类型,它派生自A和B。 最佳答案

java - Java 是否有等效的 const 引用?

这是一段代码://GameboardismadeupofSquares.AplayercanplaceGamePiecesonaSquare.publicclassCheckersBoard{publicbooleanPlaceGamePiece(GamePiecegamePiece,intnRow,intnColumn){returnm_theGameBoard[nRow][nColumn].PlaceGamePiece(gamePiece);}privateSquare[][]m_theGameBoard;}假设我正在测试PlaceGamePiece方法(使用junit)并且我需要

c++ - 如何在调用其他函数的 const 和非常量函数之间重用代码

在这个示例代码中,两个process()函数中的循环是重复的。唯一的区别是一个是const而另一个不是。有没有办法去除代码重复,使循环只存在一次?这只是一个示例,但在实际代码中,循环非常复杂,因此出于维护原因,我希望循环只存在一次。#include#includetypedefunsignedintItem;typedefstd::vectorData;structReadOnlyAction{voidaction(constItem*i){//Readitem,donotmodifystd::coutbegin();i!=d->end();i++){Item*item=*i;cb->

c++ - 为什么 const 在这个模板结构中丢失了?

已知以下函数指针有不同的类型:voidfoo_int_ref(int&);voidfoo_const_int_ref(constint&);static_assert(!std::is_same::value,"Typesshouldbedifferent");让我们考虑这个概括:templatevoidfoo(Tt){}templatestructref_vs_const_ref{typedefdecltype(foo)foo_T;typedefdecltype(foo)foo_const_T;};usingint_ref_vs_const_ref=ref_vs_const_ref;

c++ - reinterpret_cast 可以更改对象表示吗?

我对reinterpret_cast的心理模型一直是,将表达式的位序列视为不同类型,并且cppreference(注意:这不是C++标准的引用)似乎同意这一点:Unlikestatic_cast,butlikeconst_cast,thereinterpret_castexpressiondoesnotcompiletoanyCPUinstructions.Itispurelyacompilerdirectivewhichinstructsthecompilertotreatthesequenceofbits(objectrepresentation)ofexpressionasifi

c++ - 我可以通过 reinterpret_cast 将 int 的空指针转换为 long 类型吗

int*pt=0;longi=reinterpret_cast(pt);我保证为0吗?这是明确定义的还是实现定义的?我检查了c++标准,但它只说明了Apointertoadataobjectortoafunction(butnotapointertomember)canbeconvertedtoanyintegertypelargeenoughtocontainit.在这种情况下,pt不指向任何数据对象。该规则适用于这种情况吗? 最佳答案 否,i不一定是任何值。结果是实现定义的。†在C++中,指针的表示是实现定义的,包括空指针的表示