- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个这样声明的结构:
template <typename T, typename U> struct select_type;
我擅长于:
template <> struct select_type<float, double>
{
typedef double type;
};
对于多种类型,依此类推,例如 <double, float>
, <int, float>
...
我在我的一些模板函数中使用它,例如:
template <typename T, typename U, typename select<T,U>::type R >
smu::Matrix<R> operator*(const smu::Matrix<T>& a, const smu::Matrix<U>& b)
{
/* code here */
}
我尝试了几种方法来使用它,没有 R
, 没有 typename
但大多数时候我在请求 nested-name parameter before select
时出错.事实是我从来没有这样做过,我不知道我应该如何使用这个结构。谁能帮我解决这个问题?
最佳答案
这里有一些错误。你声明的方式 R
:
typename select<T,U>::type R
作为 select<T,U>::type
类型的值 .那不是你想要的-你想要R
成为那个类型。其次,R
是一个非推导上下文——它是一个模板参数,没有在任何参数中指定,所以不能推导,只能明确指定。但是你也不能真正明确地指定它,因为这违背了方便 operator*
的意义。反正。
在 C++11 及更高版本中,您可以将其设为默认类型参数:
template <typename T, typename U, typename R = typename select<T,U>::type>
smu::Matrix<R> operator*(const smu::Matrix<T>& a, const smu::Matrix<U>& b)
但是在C++03中,你不能有默认的函数模板参数,所以你只需要写出来:
template <typename T, typename U>
smu::Matrix<typename select<T,U>::type> operator*(const smu::Matrix<T>& a,
const smu::Matrix<U>& b)
{
typedef typename select<T,U>::type R;
/* rest as before */
}
关于c++ - 努力使用模板化结构选择类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36840423/
我正在尝试为我的项目创建一些单元测试,经过大量挖掘之后,我发现了Effort,这个想法很棒,它模拟数据库而不是处理伪造的DBContext,顺便说一句,很难做到正确使用复杂的架构。 但是,我将用户的电
我正在尝试对某些响应 Entity Framework 数据库上下文的类进行单元测试。为了寻求帮助,我设法找到了一个名为 Effort 的库,它似乎有点旧,而且没有很好的文档记录,但它似乎可以工作,而
这两天,Auto-GPT 爆火 https://github.com/Torantulino/Auto-GPT 它是一款让最强语言模型GPT-4能够自主完成任务的模型,让整个AI圈疯
为什么会出现这个异常?这是错误吗? 我正在使用 EF 测试库 Effort 创建我的数据库的内存实例并遇到这个有趣的场景: 打开DbContext1 将项目添加到表(不保存) 关闭DbContext1
我是一名优秀的程序员,十分优秀!