I want to do something like this:
- My library defines a struct
LibStruct
- Clients of the library build their own structs that are somehow related (in the example, I used inheritance but maybe there's a better way of expressing)
- The library has a method which does some operation using these structs
- Avoid virtual function calls (hard requirement) and avoid union/variants (soft requirement)
What I've been doing so far is this:
template <typename T /** And a bunch more **/>
struct LibStruct {
using Scalar = T;
// And a bunch more aliases
// Some data
};
template <typename T>
struct ClientStuctA : LibStruct<T> {
using Scalar = T; // Can this be avoided?
Scalar moreData;
};
struct ClientStuctB : LibStruct<double> {
using Scalar = double; // Can this be avoided?
Scalar otherData;
};
template <typename Whatever>
typename Whatever::Scalar doSomething(const Whatever& input) {
// Do stuff
return Whatever::Scalar();
}
My issue with this is that all of the client structs need to redefine all the aliases so that doSomething
can use them. Is there a way to avoid the need for that?
(Asking for C++14
, but if there's a C++17
solution I'll take that too)