Search code examples
c++c++17aliastype-deductiontemplate-variables

Template Alias, Variable Template, and auto type deduction failing to deduce template argument


While working on my class declaration I'm having some confusion on how to use alias templates and template variables within in a non class template while trying to use auto type deduction.

Signal.h

#ifndef SIGNAL_H
#define SIGNAL_H

#include <cstdint>

template<typename T>
using TimeSignal = T;

using DiscreteTime = TimeSignal<std::uint8_t>;
using ContinuousTime = TimeSignal<double>;

class Signal {
private:
    template<typename T>
    static TimeSignal<T> time_;

    double voltage_;
    double current_;

public:
    template<typename T>
    explicit Signal( TimeSignal<T> time, double voltage = 0, double current = 0 ) :
        voltage_{voltage}, current_{current}
    { time_ = time; }

     double sampleVoltage() { return voltage_; }
     double sampleCurrent() { return current_; }

     template<typename T>
     static auto atTime() { return time_; }         
};

#endif // SIGNAL_H

And I would be using it like this:

#include <iostream>
#include "Signal.h"

int main() {
    DiscreteTime t1{ 5 };
    ContinuousTime t2{ 7.5 };

    Signal s1{ t1, 3.5, 0.05 );
    Signal s2{ t2, 4.3, 0.09 );

    auto time1 = s1.atTime();
    auto time2 = s2.atTime();

    return 0;
}

I don't want to template this class, so I was thinking about having an internal variable template. Outside of the class I was trying to use a template alias to have the different "TimeSignals" be descriptive as a "DiscreteTime" is typically and integral type and a ContinousTime is a floating point or over the set of Real numbers. I was however templating the constructor of this class that takes in the TimeSignal type and wanted the class to deduce the or to auto resolve it's internal variable template to that type depending which of the two types were passed in. Finally I was trying to use auto type deduction to return that type.

I don't know if its the syntax or the usage but this has me stumped. I'm not sure how to get this to a working compile state.

This is the current compiler errors that Visual Studio 2017 is giving me.

1>------ Build started: Project: Circuit Maker Simulator, Configuration: Debug x64 ------
1>main.cpp
1>c:\...\main.cpp(15): error C2672: 'Signal::atTime': no matching overloaded function found
1>c:\...\main.cpp(15): error C2783: 'auto Signal::atTime(void)': could not deduce template argument for 'T'
1>c:\...\Signal.h(64): note: see declaration of 'Signal::atTime'
1>c:\...\main.cpp(24): error C2672: 'Signal::atTime': no matching overloaded function found
1>c:\...\main.cpp(24): error C2783: 'auto Signal::atTime(void)': could not deduce template argument for 'T'
1>c:\...\Signal.h(64): note: see declaration of 'Signal::atTime'
1>Done building project "Circuit Maker Simulator.vcxproj" -- FAILED.

The compiler error's obvious to what they are saying, but it's like they are screaming or yelling at me without any help, assistance or suggestions on how to fix or resolve this...

Edit

User rafix07 helped me quite a bit with his answer and it was helpful. I was missing a couple of things, two of them I may have eventually caught onto if I kept staring at it long enough and that was the use of the variable templates within the class needing it's template argument or parameter. The other was using the scope resolution operator in the main function to call the static function. I could of found them given some time.

The one issue that had me stumbling in circles was the fact that I had to explicitly instantiate the function template of the type I want when calling it. This is the one that would of had me pulling out my hair for ours...

After adjusting the code according to the link in his answer I'm now able to compile, however I am now getting linker errors for unresolved external symbols and it has to do with the template variables. Which shouldn't be a problem, just need to define it within a cpp file to resolve static variables.


Solution

  • First of all, atTime is static method so only way to call it is to use scope resolution operator ::. atTime takes no arguments, so T cannot be deduced, and you need to put type in template arguments list explicitly:

    auto time1 = Signal::atTime<DiscreteTime>();
    auto time2 = Signal::atTime<ContinuousTime>();
    

    In ctor of Signal and atTime function you have to specify T for which variable template is accessed:

    template<typename T>
    explicit Signal( TimeSignal<T> time, double voltage = 0, double current = 0 ) :
        voltage_{voltage}, current_{current}
    { time_<T> = time; }
    

    Full working code is here.