C++中类成员的模板实例化
Templates instantiation from class members in C++
我正在尝试创建一个数据对象(类或结构),该对象包含编译时已知值,用于实例化模板类。也许这在下面的例子中变得更加清楚:
class S
{
public:
const int j;
... constructor
// Must contain member to later instantiate templates.
// Defining these members must be forced
// Defined at compile time
}
template<int I>
class C {...}
int main(){
S s(10);
S s2(20);
// now create some class from the field in s
C<s.j> c(...)
C<s2.j> c2(...)
}
只有当成员j是静态的,这才会起作用,对吧?但是,我想创建定义S的多个实例的可能性,以便使用这种类类型。如果我使j为静态,那么所有实例只有一个可能的值。有办法绕过这个吗?
template<int I>
class S {
public:
static const int j = I;
...
}
不能将运行时值(类成员)作为模板参数传递。
相关文章:
- 静态数据成员模板专用化的实例化点在哪里
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 受约束的成员函数和显式模板实例化
- 实例化多种类型的成员函数模板
- 为什么在使用指针时不采用类成员的默认值,而不是直接实例化对象时?
- 将类成员函数的模板定义放在 CPP 文件中C++隐式实例化而不是 .H 允许吗?
- 如何实例化类的公共成员并将其作为 std::p romise 返回?
- 在实例化封闭类模板之后,我们可以声明模板类成员的部分专用化吗
- 使用 SFINAE 有选择地实例化模板的成员函数
- 静态模板成员函数的实例化?
- 访问使用接口实例化的类的私有成员
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- 参考数据成员到模板的实例化
- 类的私有成员在我的类实例化期间更改,即使他们不应该
- 如果未实例化成员模板,是否要评估static_asserts?
- 实例化与unique_ptr的类集合成员
- 实例化成员模板函数时的Buggy(?)编译器行为
- 类模板的成员函数模板找不到定义,尽管存在显式实例化。不链接
- 如何实例化C++成员字符串引用
- 通过模板参数选择子类与实例化成员变量的区别