I had previously asked these two questions:
My question pertains to the class provided in the first question which is my Signal
class.
The idea that I'm trying to convey within the class is that I do not want this class to be a template. However, I have a need to store a "time" related variable but there are two ways that this could be stored; either as an integral type: "Discrete" or as a floating point type: "Continuous".
I was thinking of using template aliasing to help simplify the code. As you can see from the code:
template<typename T>
using TimeSignal = T;
using DiscreteTime = TimeSignal<std::uint64_t>;
using ContinousTime = TimeSignal<double>;
Is how I was thinking of using template aliases.
Then within the non template class itself.
class Signal {
private:
template<typename T>
static TimeSignal<T> time_;
// ...
};
I have my template variable as a member of Signal. Now in order to have a template variable it has to be static. For purposes of testing the class, I'm currently using its constructor to instantiate and store something into this variable.
class Signal {
public:
template<typename T>
explicit Signal( TimeSignal<T> time ) {
time_<T> = time;
}
};
Now when it comes to accessing this variable I have two choices, I could access it via a static function using scope resolution operator, or I could access it via a non static member function of an object.
class Signal {
public:
template<typename T>
auto atTime() { return time_<T>; }
// or
template<typename T>
static auto atTime() { return time_<T>; }
};
Now in order for the class to compile and build correctly this line of code must de defined outside of the class in some other CPP file that is using it:
template<typename T>
TimeSignal<T> Signal::time_;
Which is understandable since the variable is a static member.
This should give you the background of my class's current structure. There are a couple of things I would like to know about this code design before I continue to add more to it.
The first and easier question is: if my code was to remain as it is; which would be the preferred way to retrieve the variable:
DiscreteTime t1{ 5 }; // 10 seconds
ContinousTime t2{ 7.9 }; // 7.9 seconds
Signal s1( t1 ); // Signal S1 using Discrete Time intervals
Signal s2( t2 ); // Signal S2 using Continuous Time
// Accessing the member variable: which is preferred:
// via static method and scope resolution?
auto time1 = Signal::atTime<Discreate>();
auto time2 = Signal::atTime<Continuous>();
// or through non static object member?
auto time1 = s1.atTime<Discrete>();
auto time2 = s2.atTime<Continous>();
The next question which is a little more important than the previous as this would effect the entire design decision is...
Is my reasoning about template aliasing correct in this kind of situation, my original thoughts or intentions are as follows:
I want the variable template within the class to be either a std::uint64_t
or a double
, but I would like to use the names above: DiscreteTime
and ContinousTime
to represent the underlying types as if they were typedefs.
If not I would like to see other possible implementations.