- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在处理我的类声明时,我对如何在尝试使用自动类型推导时在非类模板中使用别名模板和模板变量感到有些困惑。
信号.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/
运行以下代码片段,我没有收到任何错误,并且得到了预期的结果。但是,由于第二个模板实例化是不明确的 ( both type specifiers are references ),我担心这可能不是定义的
考虑以下示例: #include struct A {}; template void f() { static_assert(std::is_same_v); // #1 A&
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
我对编写模板元编程比较陌生,这可能是我找不到解决这个问题的原因。问题是这样的:我正在开发一个数学库,它有很多函数,比如确定整数或 std::initializer_list 的质数,将整数更改为罗马数
我在 Android Oreo 源代码中阅读了一些我不太理解的代码。 首先,类IOMXNode有一个函数: class IOMXNode : public IInterface { public: +
有很多关于模板参数推导的讨论和澄清,特别是引用折叠和“通用引用”。本题通过相关细节:How does auto deduce type? ,而 Scott Meyers 的这篇论文更详细,可能会提供更
我将 Java 中的许多假设带到了我对 C++ 的学习中,这似乎再次难倒了我。我没有足够的词汇量来 Eloquent 地说出我希望从以下程序中看到什么,所以我只展示它并说出我希望看到的内容: #inc
对于下面的程序,Clang 5 (trunk) 报告 IsNoexcept 不可推导,而 GCC 7.1 会出现段错误。 标准(草案)对此有何评论?这是编译器 QOI 问题吗? static_asse
我最近发现,在 lambda 中按值捕获 const 对象意味着 labmda 主体(即 lambda 的数据成员)内的变量也是 const. 例如: const int x = 0; auto fo
我是否有机会推断出 PHP Closure 参数类型信息?考虑这个例子: 5, 'b' => 10]); } else { call_user_func($closure, 5, 10);
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
对于上述 svm 的拉格朗日函数,我可以得到如下的偏导数: 但是,我不明白如何将它们插入拉格朗日以导出对偶形式? W可以被替换,但是b去哪里了? 有人可以解释一下并给出详细步骤吗? 最佳答案 你的拉格
我正在寻找一些算法、程序或函数来推断变量的创建方式,只要我提供其他变量即可。我认为计算机程序员会称之为“反编译”,而架构师会称之为“逆向工程”,但我想我不知道统计学家会怎么调用它......或者是否有
这就是我的简单类的样子。 template class A { T first; T second; public: A(T f, T s) : first(f), second(s) {}; te
这个问题在这里已经有了答案: Is it possible to figure out the parameter type and return type of a lambda? (5 个答案)
我有一个函数需要两个 std::function s 作为参数。第二个函数的参数与第一个函数的结果类型相同。 我写了一个这样的函数模板: template void examplFunction(st
O'reilly Optimizing SQL Statments Book的Explaining MySQL Explain章节,最后有这个问题。 The following is an examp
举例 template void function(T&& arg) 有人可以详细解释它是如何结束函数签名变成左值的 T& 和传入的右值的 T&& 吗?我知道不知何故(需要标准行)T -> T& 在
我正在开发用于 EMV 交易的软件,但我面临着雇佣我的公司的文档严重缺乏的问题。 其中之一是关于用于生成 ARQC 的 MKD(在第一个 GENERATE AC 期间)。我从消息请求中知道IAD如下:
我是一名优秀的程序员,十分优秀!