【发布时间】:2021-07-25 17:41:34
【问题描述】:
标题可能看起来有点混乱,所以这里有一个更详尽的解释:
我有一个模板类,它有一个向量作为成员变量。模板参数是一个结构(或类),它有一个特定的变量。这个向量的类型应该从模板参数(从这个特定的变量)派生。棘手的部分是它应该从模板参数的成员变量派生。
#include <vector>
#include <complex>
using namespace std;
struct thingA {
double variable;
//...
};
struct thingB {
complex<double> variable;
//...
};
template <class S>
class someClass {
vector< " type of S::variable " > history; // If S=thingA, make it a double, if S=tingB make it a complex<double>
}
// Usage:
someClass<thingA> myInstanceA; // (this instance should now have a vector<double>)
someClass<thingB> myInstanceB; // (this instance should now have a vector<complex<double>>)
【问题讨论】:
标签: c++ templates type-deduction