根据模板参数可以选择静态的类成员
Class members that are optionally static depending on template parameters
>想象两个类,StaticFoo
和NonStaticFoo
,它们是相同的,只是类成员在StaticFoo
中是静态的,但在NonStaticFoo
中不是静态的。
简单的例子:
class StaticFoo {
static void bar();
static int v;
};
class NonStaticFoo {
void bar();
int v;
};
是否可以分解出模板类Foo
以避免代码重复?
这样就可以使用类似的东西
using StaticFoo = Foo<true>;
using NonStaticFoo = Foo<false>;
是否可以分解出模板类 Foo 以避免代码重复?
"不">
不幸的是,static
不像noexcept
那样通过表达式参数化,所以你不能说static(false)
这样的话。
如果不使用宏,您将被迫专用化模板,以便您可以在专用化中拥有static
成员:
template<bool = false>
class Foo{
void bar();
int v;
};
template<>
class Foo<true>{
static void bar();
static int v;
};
相关文章:
- 如何使用默认参数等选择模板专业化
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- #为""定义宏;静态";针对不同的上下文
- cmake如何在fedora工作站中找到boost静态库包
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- 根据模板参数可以选择静态的类成员
- CMake FindPackage(PythonLibs):CMake 是否优先选择动态库或静态库?
- 用于 LINQ 静态方法的 C++/CLI Lambda 选择器
- 如何让编译器在指针上选择模板的静态数组版本
- 对于c++中的单个静态变量,有没有比类更好的选择
- 如何在共享库和静态库之间进行选择
- Visual C++ 使用成员选择运算符(. 或 –>)访问静态成员函数
- c++中方法和对象选择的静态多态性
- 每次选择参数时调用一次的静态变量和函数
- 如何从另一个仅静态选择满足特定类型规则的索引的元组实例创建元组实例
- 如何在“选择文件夹”对话框中为静态控件的背景着色