草庐IT

static_casting

全部标签

c++ - static_assert 和英特尔 C++ 编译器

阅读cppreference.com:Astaticassertdeclarationmayappearatblockscope(asablockdeclaration)andinsideaclassbody(asamemberdeclaration)好的,现在我有以下代码:structfoo_t{staticconstexprstd::size_tmaxAlignment(){//Thisisjustasample;Iremovedrealcodefromthismethod.returnstd::max(alignof(__m128),__alignof(__m256));}sta

c++ - static_assert 无法将 const char* 模板参数识别为 constexpr : g++ bug?

考虑以下定义。charright_string[]="::right_one.";charwrong_string[]="::wrong_one.";templatevoidf(){static_assert(str==::right_string,"Passme::right_string!");}structTest{staticconstexprcharright_string[]="template_struct::right_one";staticconstexprcharwrong_string[]="template_struct::wrong_one";template

java - Kotlin for android 和 static final 内部类

我正在尝试在Kotlin上重写我的android应用程序,并且在转换ContentProvider契约(Contract)类时遇到了问题。所以我有简单的契约(Contract)类(内容类型和内容uris等默认内容被省略):publicfinalclassContract{publicstaticfinalclassContacts{publicstaticfinalStringNAME="Name"publicstaticfinalStringBIRTH="Birth"publicstaticfinalStringIMAGE="Image"}}如果我理解正确,在Kotlin中我们没有静

java - Kotlin for android 和 static final 内部类

我正在尝试在Kotlin上重写我的android应用程序,并且在转换ContentProvider契约(Contract)类时遇到了问题。所以我有简单的契约(Contract)类(内容类型和内容uris等默认内容被省略):publicfinalclassContract{publicstaticfinalclassContacts{publicstaticfinalStringNAME="Name"publicstaticfinalStringBIRTH="Birth"publicstaticfinalStringIMAGE="Image"}}如果我理解正确,在Kotlin中我们没有静

c++ - 类型特征 : Check if reference member variable is static or not

我想检查一个类的成员变量是否是静态的。使用std::is_member_pointer适用于除引用成员之外的所有类型。#includestructA{intfoo;};structB:A{};structC{staticintfoo;};structD:C{};structE{int&foo;};structF{staticint&foo;};static_assert(std::is_member_pointer::value,"No");static_assert(std::is_member_pointer::value,"No");static_assert(!std::is_

c++ - 工厂、unique_ptr 和 static_cast

考虑具有基对象、派生接口(interface)和最终对象的多态类://baseobjectstructobject{virtual~object()=default;};//interfacesderivedfrombaseobjectstructinterface1:object{virtualvoidprint_hello()const=0;templatestaticvoidon_destruction(object*/*ptr*/){std::cout在实际用例中,最终对象是通过插件系统定义的,但这不是重点。请注意,我希望能够在销毁对象时调用on_destruction(请参阅

c++ - 仅在实例化时使用 `static_assert` 使类模板特化无法编译的安全、符合标准的方法?

假设我们想要制作一个模板类,它只能用数字实例化,否则不能编译。我的尝试:#includetemplatestructOnlyNumbers{public:structC{};static_assert(std::is_same::value,"Tisnotarithmetictype.");//OnlyNumbers*ptr;};templatestructOnlyNumbers>>{};structFoo{};intmain(){OnlyNumbers{};//Compiles//OnlyNumbers{};//Error}Livedemo-所有三个主要编译器似乎都按预期工作。我知道

c++ - 这是 const_cast 未定义的行为吗?

我想知道以下是否是未定义的行为//Case1:int*p=0;intconst*q=*const_cast(&p);//Case2:(Ithinkthisisthesame)int*p=0;intconst*const*pp=&p;intconst*q=*pp;读取int*是否是未定义的行为,就好像它是intconst*一样?我认为这是未定义的行为,但我以前认为通常只添加const是安全的,所以我不确定。 最佳答案 资格方面,没问题。将每个表达式拆分为一个语句:int*p=0;//okint**addrp=&p;//okintcon

c++ - 不使用 reinterpret_cast 读取二进制数据

只是因为在我编写读取二进制STL文件的程序之前,我从未读取过二进制文件。我使用带有char*参数的ifstream读取成员。为了将我的结构转换为char*,我使用了reinterpret_cast。但据我所知,我读过的每本关于C++的书都说过类似“除非你必须使用,否则不要使用reinterpret_cast”之类的话。有什么更好的方法来读取二进制数据,不一定是直接读取,但最终读取到一个结构中并且不需要reinterpret_cast?主要功能:std::ifstreamin(cmdline[1].c_str(),std::ios::binary);in.seekg(80,std::if

c++ - 几乎 pod 数据的 reinterpret_cast(布局兼容性是否足够)

我正在尝试了解static_cast和reinterpret_cast。如果我是正确的,标准(9.2.18)表示pod数据的reinterpret_cast是安全的:ApointertoaPOD-structobject,suitablyconvertedusingareinterpret_cast,pointstoitsinitialmember(orifthatmemberisabit-field,thentotheunitinwhichitresides)andviceversa.[Note:TheremightthereforebeunnamedpaddingwithinaPO