- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我可以对模板变量使用 enable_if(或者是否有一些可用的替代技术)。例如
typedef float Float;
typedef double Double;
template<class T>
constexpr Bool IsFloat = std::is_same_v<T, Float>;
template<class T>
constexpr Bool IsDouble = std::is_same_v<T, Double>;
template<class T>
constexpr Bool IsFloatingPoint = IsFloat<T> || IsDouble<T>;
template<class T>
using EnableIfFloatingPoint = std::enable_if_t<IsFloatingPoint<T>>;
template
<
class T,
typename = EnableIfFloatingPoint<T>
>
constexpr T Pi = T(3.1415926535897932384626433832795);
当我尝试使用 Pi<float>
时,Visual Studio 给我一个编译器错误,提示“模板参数太少” ,例如。
最佳答案
马上我建议使用 std::is_floating_point
而不是手动滚动您自己的浮点检测机制。此外,您还可以使用 _v
后缀而不是 ::value
从 C++17 开始。
正如一些评论中提到的,在变量模板上使用 SFINAE 本身没有多大意义,但您可以实现仅允许浮点类型能够采用 Pi
的值的解决方案。通过尝试将变量模板设置为类型 std::enable_if_t< std::is_floating_point<T>::value, T>
,当然只有在满足条件时才会有推导类型。
template<class T>
using EnableIfFloatingPoint = std::enable_if_t<std::is_floating_point<T>::value, T>;
template<class T>
constexpr T Pi = EnableIfFloatingPoint<T>(3.1415926535897932384626433832795);
Pi<T>
对于整数类型 T
将无法编译,因为 EnableIfFloatingPoint<T>
不会推导出任何类型,但我不会考虑这个 SFINAE。
更好的解决方案是使用 constexpr
具有适当 static_assert
的功能模板验证模板已使用“正确”类型实例化的消息。
template<class T>
constexpr T Pi()
{
using is_fp = std::is_floating_point<T>;
static_assert( is_fp::value, "Pi must be instantiated with floating point types.");
return T{3.1415926535897932384626433832795};
}
关于c++ - 变量模板和 std::enable_if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36657585/
我正在使用这样的模板别名: template ::value>::type> using vec2 = std::pair; template ::value>::type> using vec3 =
基本问题陈述 我正在学习 SFINAE。我尝试了一个非常简单的 enable_if : // 1: A foo() that accepts arguments that are derived fr
在language reference of std::enable_if at cppreference包括以下注释 Notes A common mistake is to declare two
我对 SFINAE 有基本的了解,我想我理解了很多关于如何 std::enable_if 的例子。利用它来选择函数模板特化,但我很难理解它如何用于类模板。 以下例子来自cppreference.com
这有什么问题? 我认为这应该在使用 enable if 时起作用??? 帮助?? 不应该排除第二个构造函数吗? #include #include #include template class
我试图在模板类中声明函数,以便函数声明依赖于模板类型参数。 template struct Block { static bool parse(int32_t index,
我正在尝试一种基于类模板参数来专门化成员函数的方法,而不必在类上使用 SFINAE(并导致代码重复或创建另一个类)。 由于两个模板参数不能是可选的,并且参数 enable_if 在指南中是不受欢迎的,
我遇到一个问题,未知代码正在使用试图在编译时和运行时取消引用类型的元模板。这意味着,它们遍历指针层次结构直到找到匹配器。 现在通常这很好用。但是当用户传递类似的东西时: typedef struct
我试图了解 std::enable_if 是如何工作的 inn 模板参数。 #include #include #include using namespace std; class Inter
有谁知道为什么下面的代码可以编译 static const size_t CONSTANT = /* ... */; template = 0 > res_type foo() { // ...
我有一个类定义为: template class AdjacencyList; 其中 V 和 E 分别是顶点和边值的类型。 我目前正在尝试在 AdjacencyList 中定义以下成员函数: std
我有以下代码无法在 VC2010 上编译: #include using namespace std; template typename enable_if::type foo() { retu
为什么编译器对 std::tuple 的访问者具有以下定义顺序很重要 namespace TupleVisit{ //This function SHOULD BE DEFINED S
我想使用 type_traits 是否通过 shared_ptr 重载。 struct A { A(int i) : x(i) {} int x; }; int main() {
作为一个实验,我试图使一个没有参数的 void 成员函数根据类模板参数改变行为: #include #include template class MyClass { public: void
我正在使用 enable_if 语句来删除考虑中的可能方法。 #include "gmpxx.h" #include template struct is_ring_field { }; temp
是否有任何“更干净”的方式(我的意思是减少重复代码)来编写以下内容? template class Test { struct Foo1 { int a; }; struct F
在C ++中,请考虑以下示例: template struct q; template struct q { q() { cout struct q {
我对 std::enable_if 很陌生,想知道如何使用它。我有一个模板类: template class foo { } 现在我只想在 a + b 等于 10 时实例化它。我可以使用 std::e
我最近遇到了一个有趣的 enable_if 用法版本,它用于有条件地启用具有更好可读性的函数,因为该函数的返回类型不是 enable_if 的一部分(请参阅 cleaner_usage): #incl
我是一名优秀的程序员,十分优秀!