std::enable_if带非类型模板参数
std::enable_if With Non-Type Template Parameters
如何在std::enable_if
中使用非类型模板参数比较?我不知道如何再做一次。(我曾经有过这个工作,但我失去了代码,所以我不能回头看它,我找不到我找到答案的帖子。)
提前感谢您对这个话题的帮助。
template<int Width, int Height, typename T>
class Matrix{
static
typename std::enable_if<Width == Height, Matrix<Width, Height, T>>::type
Identity(){
Matrix ret;
for (int y = 0; y < Width; y++){
elements[y][y] = T(1);
}
return ret;
}
}
编辑:修正注释中缺少的括号
这完全取决于您想在无效代码中引发什么样的错误/失败。这里有一种可能性(撇开明显的static_assert(Width==Height, "not square matrix");
)
(98年c++风格)
#include<type_traits>
template<int Width, int Height, typename T>
class Matrix{
public:
template<int WDummy = Width, int HDummy = Height>
static typename std::enable_if<WDummy == HDummy, Matrix>::type
Identity(){
Matrix ret;
for (int y = 0; y < Width; y++){
// elements[y][y] = T(1);
}
return ret;
}
};
int main(){
Matrix<5,5,double> m55;
Matrix<4,5,double> m45; // ok
Matrix<5,5, double> id55 = Matrix<5,5, double>::Identity(); // ok
// Matrix<4,5, double> id45 = Matrix<4,5, double>::Identity(); // compilation error!
// and nice error: "no matching function for call to ‘Matrix<4, 5, double>::Identity()"
}
编辑:在c++ 11的代码可以更紧凑和清晰,(它在clang 3.2
工作,但不是在gcc 4.7.1
,所以我不确定它是多么标准):
(c++ 11风格)
template<int Width, int Height, typename T>
class Matrix{
public:
template<typename = typename std::enable_if<Width == Height>::type>
static Matrix
Identity(){
Matrix ret;
for(int y = 0; y < Width; y++){
// ret.elements[y][y] = T(1);
}
return ret;
}
};
EDIT 2020: (c++ 14)
template<int Width, int Height, typename T>
class Matrix{
public:
template<typename = std::enable_if_t<Width == Height>>
static Matrix
Identity()
{
Matrix ret;
for(int y = 0; y < Width; y++){
// ret.elements[y][y] = T(1);
}
return ret;
}
};
(C + + 20) https://godbolt.org/z/cs1MWj
template<int Width, int Height, typename T>
class Matrix{
public:
static Matrix
Identity()
requires(Width == Height)
{
Matrix ret;
for(int y = 0; y < Width; y++){
// ret.elements[y][y] = T(1);
}
return ret;
}
};
我在这里找到了我问题的答案:使用c++ 11 std::enable_if来启用…
在我的解决方案中,SFINAE发生在我的模板化返回类型中,因此使函数模板本身有效。在此过程中,函数本身也被模板化。
template<int Width, int Height, typename T>
class Matrix{
template<typename EnabledType = T>
static
typename Matrix<Width, Height,
typename std::enable_if<Width == Height, EnabledType>::type>
Identity(){
Matrix ret;
for (int y = 0; y < Width; y++){
ret.elements[y][y] = T(1);
}
return ret;
}
}
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- C++ 模板:重载时找不到基类类型参数方法