具有类型条件的 C++ 模板
C++ template with condition for type
我有 class 模板。我想为 class 方法之一添加模板条件。
这个想法是针对浮点类型我想要单独的 passed 方法来用一些 epsillon 值调用它。
可能吗?我想要的例子:
template<typename ValueType>
class Comparator {
public:
...
bool passed(ValueType actualValue);
template<
typename ValueType,
typename = std::enable_if_t<
std::is_floating_point<std::remove_reference_t<ValueType>>::value
>
>
bool passed(ValueType actualValue, ValueType eps) { ... }
...
};
环境:
德比安 11,
C++14,
gcc (Debian 10.2.1-6) 10.2.1 20210110
你已经完成大部分了。为了重载 SFINAE,您需要使用具有指定默认值的非类型模板参数,而不是使用默认类型模板参数。
因此,我们将这两个函数都更改为使用具有默认值的非类型参数,并且只是否定条件,例如:
template<typename ValueType>
class Comparator {
public:
...
template<
typename T = ValueType,
std::enable_if_t<
!std::is_floating_point<std::remove_reference_t<ValueType>>::value,
// ^- not here, so only non-floating point types allowed
bool> = true // set the bool to true, this lets us call the function without providing a value for it
>
bool passed(T actualValue) { non floating point code here }
template<
typename T = ValueType,
std::enable_if_t<
std::is_floating_point<std::remove_reference_t<T>>::value,
bool> = true // set the bool to true, this lets us call the function without providing a value for it
>
bool passed(T actualValue, T eps) { floating point code here }
是的,你可以这样做:
#include <type_traits>
template<typename ValueType>
class Comparator {
public:
template<
typename U = ValueType,
typename = std::enable_if_t<
!std::is_floating_point<std::remove_reference_t<U>>::value
>
>
bool passed(ValueType actualValue);
template<
typename U = ValueType,
typename = std::enable_if_t<
std::is_floating_point<std::remove_reference_t<U>>::value
>
>
bool passed(ValueType actualValue, ValueType eps);
};
我有 class 模板。我想为 class 方法之一添加模板条件。
这个想法是针对浮点类型我想要单独的 passed 方法来用一些 epsillon 值调用它。
可能吗?我想要的例子:
template<typename ValueType>
class Comparator {
public:
...
bool passed(ValueType actualValue);
template<
typename ValueType,
typename = std::enable_if_t<
std::is_floating_point<std::remove_reference_t<ValueType>>::value
>
>
bool passed(ValueType actualValue, ValueType eps) { ... }
...
};
环境: 德比安 11, C++14, gcc (Debian 10.2.1-6) 10.2.1 20210110
你已经完成大部分了。为了重载 SFINAE,您需要使用具有指定默认值的非类型模板参数,而不是使用默认类型模板参数。
因此,我们将这两个函数都更改为使用具有默认值的非类型参数,并且只是否定条件,例如:
template<typename ValueType>
class Comparator {
public:
...
template<
typename T = ValueType,
std::enable_if_t<
!std::is_floating_point<std::remove_reference_t<ValueType>>::value,
// ^- not here, so only non-floating point types allowed
bool> = true // set the bool to true, this lets us call the function without providing a value for it
>
bool passed(T actualValue) { non floating point code here }
template<
typename T = ValueType,
std::enable_if_t<
std::is_floating_point<std::remove_reference_t<T>>::value,
bool> = true // set the bool to true, this lets us call the function without providing a value for it
>
bool passed(T actualValue, T eps) { floating point code here }
是的,你可以这样做:
#include <type_traits>
template<typename ValueType>
class Comparator {
public:
template<
typename U = ValueType,
typename = std::enable_if_t<
!std::is_floating_point<std::remove_reference_t<U>>::value
>
>
bool passed(ValueType actualValue);
template<
typename U = ValueType,
typename = std::enable_if_t<
std::is_floating_point<std::remove_reference_t<U>>::value
>
>
bool passed(ValueType actualValue, ValueType eps);
};