草庐IT

auto_cast

全部标签

c++ - 警告 : auto-importing has been activated without --enable-auto-import specified on the command line

我的环境:QtCreator2.3.1Qt4.7.4(32位)Windows7旗舰版(64位)尝试在QtforWindows中重建项目时,我遇到以下编译器警告:warning:auto-importinghasbeenactivatedwithout--enable-auto-importspecifiedonthecommandline.Thisshouldworkunlessitinvolvesconstantdatastructuresreferencingsymbolsfromauto-importedDLLs.发出此警告的项目包含一个DLL文件。尽管有警告,DLL中的类和函数

c++ - 目标类不明确的 Dynamic_cast

我刚刚在读Stroustrup的新书。在第22.2.2章中,他讨论了dynamic_cast问题。我自己写的测试代码如下:classStorable{public:inti;virtualvoidr(){};Storable(){i=1;};};classComponent:publicvirtualStorable{public:Component(){i=1;};};classReceiver:publicComponent{public:Receiver(){i=2;};};classTransmitter:publicComponent{public:Transmitter()

c++ - 试图理解 auto_ptr

我试图了解有关auto_ptr类如何工作的某些细节。假设您有以下类(class)(我是在一个网站上找到的,该网站上有人解释了赋值运算符的要点)。classTFoo:publicTSuperFoo{auto_ptrfBar1;auto_ptrfBar2;public:TFoo&TFoo::operator=(constTFoo&that);//variousothermethoddefinitionsgohere...}现在是赋值运算符的实现。TFoo&TFoo::operator=(constTFoo&that){if(this!=&that){auto_ptrbar1=newTBar

c++ - 带有显式右值转换运算符的 static_cast

我正在编写一个简单的包装类,我想为包装类型提供显式转换运算符。以下代码使用gcc编译良好classwrap{doublevalue;public:explicitwrap(doublex):value(x){}explicitoperatordouble&&()&&{returnstd::move(value);}};intmain(){wrapw(5);double&&x(std::move(w));//okdouble&&y=static_cast(std::move(w));//clangreportsanerrorhere}但是clang报告error:cannotcastfr

c++ - 为什么不能形成对 'decltype(auto)' 的引用

intmain(){decltype(auto)&&a=100;}以上代码,在GCC和Clang中出错。intmain(){decltype(int)&&a=100;}此代码正确。在N4296中,在§8.3.2/6Ifatypedef(7.1.3),atypetemplate-parameter(14.3.1),oradecltype-specifier(7.1.6.2)denotesatypeTRthatisareferencetoatypeT,anattempttocreatethetype“lvaluereferencetocvTR”createsthetype“lvaluere

c++ - 为什么使用reinterpret_cast 将char* 转换为结构似乎可以正常工作?

人们说相信reinterpret_cast将原始数据(如char*)转换为结构是不好的。例如,对于结构structA{unsignedinta;unsignedintb;unsignedcharc;unsignedintd;};sizeof(A)=16和__alignof(A)=4,完全符合预期。假设我这样做:char*data=newchar[sizeof(A)+1];A*ptr=reinterpret_cast(data+1);//+1istoensureitdoesn'tpointsto4-bytealigneddata然后复制一些数据到ptr:memcpy_s(sh,sizeo

c++ - boost::lexical_cast<signed char> 无法处理负数?

这个简短的C++程序的行为方式让我感到困惑:#include#include#include#includeintmain(void){signedcharc=-2;assert(c==-2);c=boost::lexical_cast(std::string("-2"));std::cout使用g++5.2.1和boost-1.58.0,我得到:terminatecalledafterthrowinganinstanceof'boost::exception_detail::clone_impl>'what():badlexicalcast:sourcetypevaluecouldn

python - Cython 扩展类 : How do I expose methods in the auto-generated C struct?

我现有的C++代码定义了一些我需要使用的类,但我需要能够将这些类发送到Python代码。具体来说,我需要在C++中创建类实例,创建Python对象作为这些C++对象的包装器,然后将这些Python对象传递给Python代码进行处理。这只是一个更大的C++程序的一部分,因此最终需要使用C/PythonAPI在C++中完成。为了让我的生活更轻松,我使用Cython定义扩展类(cdef类)作为我的C++对象的Python包装器。我使用的是典型格式,其中cdef类包含指向C++类的指针,然后在创建cdef类实例时对其进行初始化。因为如果我有一个现有的C++对象要包装,我也希望能够替换指针,所以

c++ - static_cast<T&&>(t) 编译速度比 std::forward<T>(t) 快?

最近,我在这里阅读了range-v3的提交评论:https://github.com/ericniebler/range-v3/commit/a4829172c0d6c43687ba213c54f430202efd7497提交消息说,marginallyimprovecompiletimesbyreplacingstd::forwardwithstatic_cast我知道std::forward(t)返回static_cast(t),按照标准。我也知道有时static_cast(t)当T&&t时会正常工作是通过引用折叠规则的通用引用(或转发引用)。我感兴趣的是提交消息说static_c

c++ - 将整数类型转换为枚举 : functional cast vs initialization

假设有一个这样的枚举:enumfoo:int{first,second}然后我使用它如下:foof(1);//error:cannotinitializeavariableoftype'foo'withanrvalueoftype'int'foof=foo(1);//OK!我想知道这两者有什么区别?我知道第二个版本可以看作是函数式转换,但为什么这会有什么不同?例如,如果我这样做:classBar{};Barb=Bar(1);//nomatchingconversionforfunctional-stylecastfrom'int'to'Bar'我显然得到了一个有意义的错误。因此,这让我