草庐IT

static-compilation

全部标签

c++ - 带有转换运算符的类上的 static_cast

我刚刚遇到这种行为,我很难理解为什么这不起作用。enumclassTestEnum{Foo,Bar};classMyClass{public:operatorTestEnum(){returnm_enum;}TestEnumm_enum=TestEnum::Foo;}MyClasstheClass;intenumValue=static_cast(theClass);//doesnotwork,conversionoperatornotcalledintenumValue=static_cast(static_cast(theClass))//worksasexpected我知道编译器

c++ - reinterpret_cast<int*>(char*) 与 static_cast<int*>(static_cast<void*>(char*)) - 使用哪个?

当你动态分配了一个char*类型的缓冲区并想将它转换为特定类型时,你是否应该使用类似的东西reinterpret_cast(char*)或者类似的东西static_cast(static_cast(char*))为什么?我个人很想使用后者,因为对我来说,它并不是真正的数据“重新解释”(而只是一种分配缓冲区的机械方式)而且它看起来不像是一个来源错误的方式可能与典型的reinterpret_cast相同,但这是正确的直觉吗? 最佳答案 AccordingtoDaveAbrahams,使用链式static_casts是强制指针类型的正确、

c++ - static_cast 和临时创建(最终版)

先决条件:要理解这个问题,请先阅读以下问题及其答案:Castauto_ptrtoauto_ptr在Castauto_ptrtoauto_ptr史蒂夫回答说,“您的static_cast会将auto_ptr复制到一个临时文件,因此aS将被重置,当临时文件(在语句末尾)时,资源将被销毁。”我对static_cast时临时创建的过程很感兴趣叫做。我想要我可以跟踪的代码以查看此效果。我不能使用static_cast>...因为它不能被编译,所以我需要写一些模拟类而不是auto_ptr并观看临时创建的过程。我也明白临时创建与复制构造函数调用密切相关。auto_ptr的所有权丢失是通过设置_rad

c++ - 为什么我收到错误 : initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code?

我是C++的新手,在盯着它看了太久之后终于放弃了尝试编译它。编译器似乎出于某种原因拒绝了头文件中的构造函数原型(prototype)......我无法弄清楚它有什么问题。项目.h:#ifndefITEM_H_#defineITEM_H_classItem{public:Item(int);//ThislineiswhatEclipsekeepsflaggingupwiththeerrorinthetitlevirtual~Item();Item*getNextPtr();intgetValue();voidsetNextPtr(Item*);};#endif/*ITEM_H_*/在我的

c++ - 警告 : non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

这个问题在这里已经有了答案:C++ArrayInitializersWarnings(2个答案)关闭8年前。我刚刚在我的机器上安装了最新版本的cygwin和eclipseluna。它工作正常,我能够运行我的项目。但是,当我构建它们时,我收到了我不明白的警告。例如,这是我从“c++Primer”一书的网站上获得的头文件“Sales_item.h”的警告:warning:defaultedanddeletedfunctionsonlyavailablewith-std=c++11or-std=gnu++11[enabledbydefault]Sales_item()=default;^..

c++ - static_pointer_cast<Derived> pReallyABase = static_pointer_cast<Derived>(pBase) 有效!为什么?

这个问题在这里已经有了答案:Downcastingusingthe'static_cast'inC++(3个答案)关闭8年前。我不明白为什么会这样。pReallyABase是一个向下转换的shared_pointer,它指向一个基类实例。我理解为什么编译器让我调用pReallyABase->onlyForDerived()因为我将它定义为派生类指针,但是当我尝试使用该指针调用派生类函数时为什么没有出现运行时错误?classBase{public:virtualstringwhatAmI(){return"IamaBase";}};classDerived:publicBase{publ

c++ - 索引上的 static_assert 在编译时知道

有没有办法静态断言编译时已知的索引,否则在运行时断言?示例:templateclassFoo{T_data[Dim];public:constT&operator[](intidx)const{static_assert(idxfoo;foo[0];foo[1];foo[2];//compilererrorfor(inti=0;i1}return0;} 最佳答案 您可以简单地抛出异常或断言。它将在constexpr上下文中编译失败。这仅在可以在constexpr上下文中评估抛出条件时才有效。请注意,某些版本的gcc中有一个错误会阻止

c++ - 为什么 GCC 不允许我创建 `inline static std::stringstream` ?

我会直接去MCVE:#includestructA{inlinestaticstd::stringstreamss;};海湾合作委员会7.2和7.1refusetocompile它有以下错误:error:nomatchingfunctionforcallto'std::__cxx11::basic_stringstream::basic_stringstream()'inlinestaticstd::stringstreamss;^~Infileincludedfromblah:1:0:/opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0

显式类型转换(C 风格转换)的 C++ 转换符号和 static_cast 的多种解释

Paragraph4of[expr.cast](在撰写本文时可用的最新C++标准草案中)描述了C样式转换的行为如下:Theconversionsperformedbyaconst_­cast,astatic_­cast,astatic_­castfollowedbyaconst_­cast,areinterpret_­cast,orareinterpret_­castfollowedbyaconst_­cast,canbeperformedusingthecastnotationofexplicittypeconversion.Thesamesemanticrestrictionsan

c++ - boost static_vector 而不是 std::is_trivially_destructible

根据thisexample(左例)#include#includestructX{intk;std::arraya;boost::container::static_vectorb;~X()=default;};inthuh(){std::arrayx;return0;}看起来像boost::container::static_vector当T时可以轻易破坏是(当b被销毁时,不会在X上循环)。huh优化为xoreax,eax;ret(即return0不遍历数组。当我改用具有非平凡析构函数的包含类型时(右例)#include#includestructY{~Y();};structX{i