草庐IT

static-classes

全部标签

Java 等效于 C++ 模板 <int val> class foo{ };

我是Java的新手,正在尝试做类似于以下C++模板的事情:templateclassFoo{....};具有不同value_的实例化行为相同,但具有不同value_的两个实例之间的交互将取决于value1和value2。特别是,如果某些函数中的value1!=value2会导致编译时失败。一种可能的解决方案可能是将此类值存储在java类中并抛出运行时异常,但编译时检测会很有用。 最佳答案 Java最接近模板的是generics,但它严格限于强制使用“正确的”类型。Java没有任何基于值的模板机制。您最好的办法是通过为您感兴趣的值创建

c++ - 为什么 static const char * template struct 成员没有初始化

我有一些C++11模板代码,我正在尝试移植到VisualC++Compiler2015。原始代码工作得很好,但是我需要重写它以解决constexpr的问题。Theoriginalcode(simplifiedexample)#includestructString{staticconstexprconstchar*value{"STRING"};};templateclassDerived{public:staticconstexprconstchar*value{Base::value};};templatestructFoo{staticconstexprconstchar*val

C++ 嵌套模板 : inaccessible static method

我在以下代码中遇到了一个奇怪的(或者可能不是)错误:templateclassRegistrer{public:Registrer(){Registry::register(T::instance);}};templateclassRegisteringClass{private:staticconstRegistrerREGISTRER;public:RegisteringClass(){Q_UNUSED(REGISTRER);/*forcestaticinstantiation*/}staticconstWhatEver*instance(){staticTINSTANCE;ret

c++ - 如何在一个类(class)中正确管理2个不同类型的容器?

我有以下(有点伪)代码,它处理2种不同(但有点相似)类型的2个容器,我讨厌这些重复的添加和删除(以及我的真实代码中的2个搜索函数)classPureAbstractClass{public:virtualcharFunc()=0;}classPureOpt1:PureAbstract{public:virtualintFOption1(A,B,C)=0;//Notice'C'}classPureOpt2:PureAbstract{public:virtualintFOption2(A,B,D)=0;//Notice'D'}classHandler{public:voidAdd(Pure

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++ 未解析的外部符号(public static int)

当我尝试编译它时,我得到了这个链接器错误:LNK2001unresolvedexternalsymbol"public:staticintHooksXD::night"(?night@HooksXD@@2HA)Theheaderisthis:classHooksXD{public:staticvoidXD3();staticintnight;staticintnight2;};变量是公共(public)的而不是私有(private)的,因为我需要从不在同一个类中的其他voids访问它们。cpp文件:HooksXDlmao;voidHooksXD::XD3(){//thisvoidwil

C++ 销毁顺序 : Calling a field destructor before the class destructor

有没有办法在类析构函数之前调用字段析构函数?假设我有2个类Small和Big,Big包含一个Small的实例作为它的字段因此:classSmall{public:~Small(){std::cout当然,这会在小析构函数之前调用大析构函数:BigdestructorSmalldestructor我需要在Big析构函数之前调用Small析构函数,因为它会为Big析构函数执行一些必要的清理工作。我可以:显式调用small.~Small()析构函数。->但是,这会调用Small析构函数两次:一次显式调用,一次在Big析构函数执行后调用。有一个Small*作为字段并在Big析构函数中调用del

c++ - gcc 与 clang : noexcept parsed in unused template specialization when static casting

我正在尝试将函数指针静态转换为特定函数重载,但似乎clang仍会解析(未使用的)模板特化的noexcept语句,从而生成编译器错误。如果未使用相应的函数重载,GCC似乎并不关心noexcept。templatevoidfun(T)noexcept(T(1)){}voidfun(int){}voidfun(int*){}intmain(){inta;fun(&a);//callingworksfinefun(a);static_cast(&fun);//staticcastingdoesn't}https://godbolt.org/z/ixpl3f这里是哪个编译器出错了?当将函数指针转

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++ - SSE 与类(class)保持一致

有一些非常奇怪的问题,作为c++的初学者,我不知道为什么。structDeviceSettings{public:....somevariablesDXSizeBackbufferSize;....somemethods};structDXPoint;typedefDXPointDXSize;__declspec(align(16))structDXPoint{public:union{struct{intx;inty;};struct{intwidth;intheight;};intdataint[2];__m128im;};DXPoint(void);DXPoint(intx,in