草庐IT

static-compilation

全部标签

c++ - MSVC : what compiler switches affect the size of structs?

我有两个单独编译的DLL,一个是从VisualStudio2008编译的,一个是从matlab编译的mex文件。两个DLL都包含一个头文件。当我在一个DLL中采用sizeof()结构时,它返回48,而在另一个DLL中它返回64。我检查了/Zp开关,在两个编译中它都设置为/Zp8。还有哪些其他编译器开关可能会影响结构的大小?该结构是一个简单的POCO,没有继承,也没有虚函数。编辑结构看起来像这样:classLIBSPECSGeometry{public:std::vectorm_i;uintN;uintn_im,n_s;};在调试中,sizeof()在两种情况下都返回56,在发行版中,在

c++ - 有什么理由更喜欢 static_cast 而不是一系列隐式转换?

假设我有一个实现多个接口(interface)的类classCMyClass:publicIInterface1,publicIInterface2{};并且在该类的一个成员函数中,我需要获得一个指向这些接口(interface)之一的void*指针(IUnknown::QueryInterface()中的典型情况。典型的解决方案是使用一个static_cast来实现指针调整:void*pointer=static_cast(this);如果没有从CMyClass继承的已知类,在这种情况下是安全的。但是如果这样的类存在:classCDerivedClass:publicCUnrelat

c++ - 如何仅在实际使用成员模板时才在成员模板中进行 static_assert?

考虑这个简单的类:templateclassFoo{public:Foo(Tconst&val):_val(val){}templateFoo(Fooconst&){static_assert(false,"CannotconvertfromFootoFoo.");}operatorT&(){return_val;}operatorTconst&()const{return_val;}private:T_val;};它允许从模板类型隐式构造并隐式转换回该类型,一个简单的包装器。现在,我不想启用不相关的Foo之间的转换,由于这些隐式构造/转换,这是可能的。我可以将模板化复制构造函数设为私

c++ - C 和 C++ 中的 Const、static、extern 及其组合

很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visitthehelpcenter.关闭10年前。1)static、extern和const有何不同,它们在C和C++中的使用有何不同?(默认联动等差异)2)C中使用的头文件中允许以下声明和定义,然后包含在多个文件中。staticinttestvar=233;externintone;externintshow();intabc;constintxyz;//constintxyz=123;produceserrorconstdefinition

c++ - “static const”、 “#define” 和 “enum” 在性能和内存使用方面的区别

可能是因为#define语句的内联。我知道答案可能取决于编译器,那么假设是GCC。已有类似问题aboutC和aboutC++,但它们更多地是关于使用方面的。 最佳答案 编译器会在给定基本优化后将它们视为相同。检查起来相当容易-考虑以下C代码:#definea1staticconstintb=2;typedefenum{FOUR=4}enum_t;intmain(){enum_tc=FOUR;printf("%d\n",a);printf("%d\n",b);printf("%d\n",c);return0;}用gcc-O3编译:00

c++ - 带 static_assert() 的逗号运算符

当尝试使用static_assert作为参数来计算逗号运算符时编译失败voidfvoid(){}intmain(){inta=(1,2);//a=2intb=(fvoid(),3);//b=3intd=(,5);//^//error:expectedprimary-expressionbefore','token.OKintc=(static_assert(true),4);//^~~~~~~~~~~~~//error:expectedprimary-expressionbefore'static_assert'.Why?}看起来static_assert()在编译后甚至没有解析为vo

c++ - 将 C 风格的转换更改为 static_cast 总是安全的吗?

由于cppcheckcstyleCast样式警告,我正在尝试消除代码库中的所有C样式转换。将C风格的转换更改为static_cast总是安全的吗?安全,我的意思是,是否存在旧的C风格转换可以正常工作,但static_cast会引发错误或未定义行为的情况?type1a;type2b=(type2)a;//Cstylecasttype2b=static_cast(a);//Isthisalwaysavalidreplacementforabovecast? 最佳答案 C风格的转换通常是static_cast的组合或reinterpret

c++ - 如何将 `boost::static_visitor` 实例传递给函数

我正在使用boost::variant在我的项目中经常出现。我的同事们现在想出了传递特定boost::static_visitor实例的想法。以自定义访问类型。她有一些代码如下:#include#includetypedefboost::variantTVar;structVisitor1:publicboost::static_visitor{templateresult_typeoperator()(constT&){return42;}};structVisitor2:publicboost::static_visitor{templateresult_typeoperator(

c++ - C++中static的含义

我认为我的C++相当不错,事实证明我不是。我之前问过一个问题:C++constlvaluereferences其中一个答案中有以下代码:#includeusingnamespacestd;int&GenX(boolreset){staticint*x=newint;*x=100;if(reset){deletex;x=newint;*x=200;}return*x;}classYStore{public:YStore(int&x);int&getX(){returnmy_x;}private:int&my_x;};YStore::YStore(int&x):my_x(x){}intma

c++11:为什么 std::forward 中的 static_assert 是必需的?

在move.h中,forward有两个重载templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&__t)noexcept{returnstatic_cast(__t);}templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&&__t)noexcept{static_assert(!std::is_lvalue_reference::value,"templateargumentsubstituting_Tpisanlvalueref