Derive a templated class' member variable type from a template member type

10

The title might seem a little confusing, so here is a more thorough explanation:

I have a templated class that has a vector as a member variable. The template argument is a struct (or class) that will have one certain variable. The type of this vector shall be derived from the template argument (from this one certain variable). The tricky part is that it shall be derived from a member variable of the template argument.

#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>>)
Share
Improve this question
0

Comments

Popular posts from this blog

flutter websocket connection issue

Webpack 5 and Storybook 6 integration throws an error in DefinePlugin.js

Meaning of `{}` for return expression