模板别名,变量模板和自动类型推导无法推导模板参数 [英] Template Alias, Variable Template, and auto type deduction failing to deduce template argument

查看:204
本文介绍了模板别名,变量模板和自动类型推导无法推导模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理类声明时,我在尝试使用自动类型推导时如何在非类模板中使用别名模板和模板变量时有些困惑。

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

我会像这样使用它:

#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;
}

我不想为此类创建模板,所以我在考虑内部变量模板。在课外,我尝试使用模板别名来描述不同的 TimeSignals,因为通常以 DiscreteTime和集成类型 ContinousTime 是浮点数或在实数集合上。但是,我正在模板采用 TimeSignal 类型的此类的构造函数,并希望该类将或将其内部变量模板自动解析为该类型,具体取决于哪一个传入了两种类型。最后,我尝试使用自动类型推断来返回该类型。

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.

这是Visual Studio 2017给我的当前编译器错误。

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...

编辑

用户 rafix07 对我的回答很有帮助,这很有帮助。我遗漏了几件事,如果我盯着它看够久,我可能最终会发现其中两件事,那就是在类中使用需要模板参数或参数的变量模板。另一个是在主函数中使用范围解析运算符来调用静态函数。我可以花一些时间找到它们。

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...

根据他的答案中的链接调整代码后,我现在可以编译,但是我现在收到未解析的外部符号的链接器错误,这与模板变量有关。这应该不成问题,只需在cpp文件中定义它即可解析静态变量。

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.

推荐答案

首先, atTime 是静态方法,因此调用它的唯一方法是使用范围解析运算符 :: atTime 不接受任何参数,因此无法推断出 T ,您需要将类型明确地放在模板参数列表中:

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>();

Signal atTime 函数,您必须指定要访问其变量模板的 T

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; }

完整的工作代码在这里。

这篇关于模板别名,变量模板和自动类型推导无法推导模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆