如何在 class 模板中重载函数模板函数?
How to overload function template function inside a class template?
如何在 class 模板 Range
中重载 Contains
函数模板?
当我 运行 这段代码时,我得到如下错误:
template <typename T>
class Range {
public:
Range(T lo, T hi) : low(lo), high(hi)
{}
typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
typename std::enable_if<std::numeric_limits<T>::is_integer, bool>::type
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
};
error: ‘typename std::enable_if<std::numeric_limits<_Tp>::is_integer, bool>::type Range<T>::Contains(T, bool, bool) const’ cannot be overloaded
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
^~~~~~~~
error: with ‘typename std::enable_if<(! std::numeric_limits<_Tp>::is_integer), bool>::type Range<T>::Contains(T, bool, bool) const’
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
您也需要制作 Contains
自己的模板。例如
template <typename X>
typename std::enable_if<!std::numeric_limits<X>::is_integer, bool>::type
Contains(X value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
template <typename X>
typename std::enable_if<std::numeric_limits<X>::is_integer, bool>::type
Contains(X value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
从 C++17 开始,您可以使用 Constexpr If 而不是重载。
bool
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
{
if constexpr (!std::numeric_limits<T>::is_integer) {
// do sth
return true;
} else {
// do sth
return true;
}
}
如何在 class 模板 Range
中重载 Contains
函数模板?
当我 运行 这段代码时,我得到如下错误:
template <typename T>
class Range {
public:
Range(T lo, T hi) : low(lo), high(hi)
{}
typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
typename std::enable_if<std::numeric_limits<T>::is_integer, bool>::type
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
};
error: ‘typename std::enable_if<std::numeric_limits<_Tp>::is_integer, bool>::type Range<T>::Contains(T, bool, bool) const’ cannot be overloaded
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
^~~~~~~~
error: with ‘typename std::enable_if<(! std::numeric_limits<_Tp>::is_integer), bool>::type Range<T>::Contains(T, bool, bool) const’
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
您也需要制作 Contains
自己的模板。例如
template <typename X>
typename std::enable_if<!std::numeric_limits<X>::is_integer, bool>::type
Contains(X value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
template <typename X>
typename std::enable_if<std::numeric_limits<X>::is_integer, bool>::type
Contains(X value, bool leftBoundary = true, bool rightBoundary = true) const
{
// do sth
return true;
}
从 C++17 开始,您可以使用 Constexpr If 而不是重载。
bool
Contains(T value, bool leftBoundary = true, bool rightBoundary = true) const
{
if constexpr (!std::numeric_limits<T>::is_integer) {
// do sth
return true;
} else {
// do sth
return true;
}
}