草庐IT

explicit-conversion

全部标签

C++11: "narrowing conversion inside { }"带模数

我尝试在启用gcc和C++11的情况下编译以下代码:unsignedintid=100;unsignedchararray[]={id%3,id%5};我收到这些警告:narrowingconversionof‘(id%3u)’from‘unsignedint’to‘unsignedchar’inside{}[-Wnarrowing]seedemoonline有没有办法帮助编译器发现id%3的结果适合unsignedchar? 最佳答案 在这种特定情况下,使idconst或constexpr将解决问题:constexprunsign

c++ - 没有可用的复制构造函数或声明了复制构造函数 'explicit'

有人能解释一下为什么我在这里遇到编译错误-错误C2558:类“std::auto_ptr”:没有可用的复制构造函数或复制构造函数被声明为“显式”#include#include#includetemplatestructtest{typedefstd::auto_ptrdataptr;typedefstd::auto_ptr>testptr;test(constT&data):data_(newT(data)){};voidadd_other(constT&other){others_.push_back(testptr(newtest(other)));}private:datapt

c++ - 海湾合作委员会错误 : invalid conversion from double* to const double

我使用的是gcc4.5.0版。使用下面的简单示例,我假设得到一个错误invalidconversionfromdouble*toconstdouble*#includeusingnamespacestd;voidfoo(constdouble*a){cout为什么编译没有错误?类似的反例如下:#includeusingnamespacestd;voidfoo(constdouble**a){cout(第二个示例的解决方案:将foo定义为foo(constdouble*const*a)。感谢JackEdmonds的评论,这解释了错误消息) 最佳答案

c++ - 什么时候加载 DLL : Implicit Linking VS Explicit Linking

我认为隐式链接会在应用程序启动时立即加载DLL,因为它也称为“加载时动态链接”。但是我在下面的链接中发现了一些奇怪的解释(https://msdn.microsoft.com/en-us/library/253b8k2c(VS.80).aspx)。隐式链接Liketherestofaprogram'scode,DLLcodeismappedintotheaddressspaceoftheprocesswhentheprocessstartsupanditisloadedintomemoryonlywhenneeded.Asaresult,thePRELOADandLOADONCALLc

C++11 initializer_list 构造函数标记为 "explicit"

我能否将explicit与init-list构造函数一起使用,以确保像{a}这样的表达式不会导致意外的隐式转换?还有一个想法:应该我担心吗?编写{a}比简单地编写a不太可能出错,但另一方面,从代码中我们可能仍然不清楚我们正在构建一个通过隐式转换对象。classFoo{explicitFoo(std::initializer_listilist){/*...*/}}; 最佳答案 你不能。它确实导致意外的隐式转换。但是,意外的隐式转换是不允许的,编译器将拒绝您的程序。然而,这不会阻止编译器选择或考虑它。示例voidf(Foo);void

c++ - "Ambiguous conversion sequence"- 这个概念的目的是什么?

在N465916.3.3.1隐式转换序列中说10Ifseveraldifferentsequencesofconversionsexistthateachconverttheargumenttotheparametertype,theimplicitconversionsequenceassociatedwiththeparameterisdefinedtobetheuniqueconversionsequencedesignatedtheambiguousconversionsequence.Forthepurposeofrankingimplicitconversionsequen

c++ - Qt : After installation of VS2012 LNK1123 failure during conversion to COFF

之后我安装了VS2012我的Qt项目不再编译(我的电脑上有VS2010和VS2012)。消息:LNK1123:转换为COFF期间失败:文件无效或损坏。以前只用VS2010没问题。对于VS2012环境,建议(参见SOhere):使用EnableIncrementalLinkingNO编译卸载.net4.5因为我没有将Qt与VS2012结合使用(所以没有插件),所以我不能setuptheprojectasmentioned.我也不能卸载.net4.5(另一个项目需要)。有机会解决这个问题吗?我有一些相关的问题是否可以针对VS2012C++编译器进行编译?Hereitsays为此,我需要自己

C++:我可以做一个赋值运算符 "explicit"吗

我的任务是迁移C++类库中的错误处理概念。以前简单返回bool(成功/失败)的方法应修改为返回一个Result对象,该对象传达机器可读的错误代码和人类可读的解释(以及更多在这里无关紧要的内容)。遍历数千行代码很容易出错,因此我尝试从编译器获得对此任务的最佳支持。我的结果类在其他成员方法中有一个从代码构造结果的构造函数和代码的赋值运算符:classResult{public:typedefunsignedlongResultCode;explicitResult(ResultCodecode);//(1)Result&operator=(ResultCodecode);//(2)};备注

c++ - 在使用 Clang 编译 CRTP Singleton 时,如何解决声称缺少 "explicit instantiation declaration"的问题?

我们正在使用curiouslyrecurringtemplatepattern实现单例。但是,在最近的Clang版本中,我们收到了-Wundefined-var-template警告。建议的修复方法是添加“显式实例化声明”。我试图这样做,但后来在定义单例模板类成员变量的编译单元中出现有关“实例化后的显式特化”的错误。解决此警告突出显示的问题的适当构造是什么?简化详细信息(已删除大部分逻辑,以制作MCVE):单例基础.hh:templateclassSingletonBase{public:staticT*get_instance(){if(!instance_){instance_=T

c++ - Boost Program_Options 抛出 "character conversion failed"

我在Ubuntu14.04上,使用CMake和CLion。我正在尝试使用程序选项,以下代码取自其文档中的示例:#include#includeintmain(intac,char*av[]){namespacepo=boost::program_options;usingnamespacestd;po::options_descriptiondesc("Allowedoptions");desc.add_options()("help","producehelpmessage")("compression",po::value(),"setcompressionlevel");po::