0

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.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • 1
    About the *"static vs non-static member function"* part: The static function is clearly superior. You can call it using both `Signal::atTime` and `s1.atTime` notation. – HolyBlackCat Jul 21 '19 at 14:08
  • @HolyBlackCat Okay thank you for the clarity on that! I can then just implement the static version and not worry about the non static... – Francis Cugler Jul 21 '19 at 14:09
  • I may have a follow up question to this as well; its more of a side effect due to the nature of static members... – Francis Cugler Jul 21 '19 at 14:14
  • Why are you not using `std::chrono` for time durations? It would prevent having to add comments explaining what the units of a particular time is. `5s` doesn't need a comment saying "5 seconds". It also would allow users to pass in whatever timescale they wanted: hours, minutes, milliseconds, etc. – Nicol Bolas Jul 21 '19 at 15:47
  • @NicolBolas I have not yet implemented my time construct which will end up using chronos. The idea with the signal class is that it will sample it's voltage and or current at a given time t and it will store that time step as a Discrete - uint64_t or Continuous - double as there are two ways to sample it via Discrete Time Steps: Integral or Continuous Time floating point over the set of real numbers. The time functionality is outside of this class. – Francis Cugler Jul 21 '19 at 15:56

0 Answers0