在处理我的类声明时,我对如何在尝试使用自动类型推导时在非类模板中使用别名模板和模板变量感到有些困惑。
信号.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”通常是 integral type
而 ContinousTime
是 float 或实数集。然而,我正在模板化接受 TimeSignal
类型的此类的构造函数,并希望该类根据传入的两种类型中的哪一种来推断或自动将其内部变量模板解析为该类型。最后,我尝试使用自动类型推导来返回该类型。
我不知道它的语法或用法,但这让我很困惑。我不确定如何让它进入工作编译状态。
这是 Visual Studio 2017 给我的当前编译器错误。
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.
他们所说的编译器错误很明显,但就好像他们在没有任何帮助、帮助或建议来解决或解决这个问题的情况下对我尖叫或大喊大叫......
编辑
用户 rafix07他的回答对我帮助很大,很有帮助。我遗漏了一些东西,如果我一直盯着它看足够长的时间,我可能最终会发现其中的两个东西,那就是在需要它的模板参数或参数的类中使用变量模板。另一个是在主函数中使用范围解析运算符来调用静态函数。如果给我一些时间,我可能会找到它们。
让我陷入困境的一个问题是我必须在调用它时显式实例化我想要的类型的函数模板。这是一个让我为我们的头发拔掉头发的人......
根据他的回答中的链接调整代码后,我现在可以编译了,但是我现在收到未解析的外部符号的链接器错误,这与模板变量有关。这应该不是问题,只需要在 cpp 文件中定义它来解析静态变量。
最佳答案
首先,atTime
是静态方法,因此调用它的唯一方法是使用范围解析运算符 ::
。 atTime
不接受参数,因此无法推导出 T
,您需要将类型显式放入模板参数列表中:
auto time1 = Signal::atTime<DiscreteTime>();
auto time2 = Signal::atTime<ContinuousTime>();
在 Signal
和 atTime
函数的构造函数中,您必须指定访问变量模板的 T
:
template<typename T>
explicit Signal( TimeSignal<T> time, double voltage = 0, double current = 0 ) :
voltage_{voltage}, current_{current}
{ time_<T> = time; }
关于c++ - 模板别名、变量模板和自动类型推导无法推导模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57128228/