SFINAE 用作返回类型,但不是参数类型
SFINAE works as return type, but not parameter type
我试图使用enable_if
来避免重复代码。只要放在返回类型中,它就可以正常工作,但如果它位于参数中,则不能。在它作为重复关闭之前,我得到的错误不是重新定义,而是"没有匹配的调用函数"。这是我使用 VS2015 和 g++ 7.2.0 (mingw( 的 MCVE(不是"C",也不是"M"(:
#include <cmath>
#include <array>
#include <algorithm>
template <typename T, size_t M, size_t N>
class Matrix
{
public:
static const size_t ROWS = M;
static const size_t COLS = N;
typedef T SCALAR;
SCALAR operator[](const size_t index) const
{
static_assert((COLS == 1 || ROWS == 1), "operator[] is only for vectors (single row or column).");
return m_elements.at(index);
}
SCALAR& operator[](const size_t index)
{
static_assert((COLS == 1 || ROWS == 1), "operator[] is only for vectors (single row or column).");
return m_elements.at(index);
}
std::array<T, M * N> m_elements;
};
template <typename T, size_t N, size_t M>
static inline T Length(
const Matrix<typename std::enable_if<(M == 1 || N == 1), T>::type, N, M> & input)
{
T value = 0;
for (size_t i = 0; i < std::max(N, M); ++i)
{
value += (input[i] * input[i]);
}
return std::sqrt(value);
}
template <typename T, size_t M, size_t N>
static inline
Matrix<typename std::enable_if<(M == 3 && N == 1) || (M == 1 && N == 3), T>::type , M, N>
CrossProduct(const Matrix<T, M, N> & a, const Matrix<T, M, N> & b)
{
Matrix<T, M, N> result;
result[0] = a[1] * b[2] - a[2] * b[1];
result[1] = a[2] * b[0] - a[0] * b[2];
result[2] = a[0] * b[1] - a[1] * b[0];
return result;
}
Matrix<double, 1, 1> m11;
Matrix<double, 3, 1> m31;
Matrix<double, 1, 3> m13;
Matrix<double, 3, 3> m33;
auto l0 = Length(m11); // Should work, but doesn't: no matching function for call to 'Length(Matrix<double, 1, 1>&)'
auto l1 = Length(m31); // Should work, but doesn't: no matching function for call to 'Length(Matrix<double, 3, 1>&)'
auto l2 = Length(m13); // Should work, but doesn't: no matching function for call to 'Length(Matrix<double, 1, 3>&)'
//auto l3 = Length(m33); // Shouldn't work, and doesn't: no matching function for call to 'Length(Matrix<double, 3, 3>&)'
auto v1 = CrossProduct(m13, m13); //Works, as expected
//auto v2 = CrossProduct(m11, m11); // As expected: enable_if.cpp:71:32: error: no matching function for
// call to 'CrossProduct(Matrix<double, 1, 1>&, Matrix<double, 1, 1>&)'
如果我将Length
的签名更改为
static inline typename std::enable_if<(M == 1 || N == 1), T>::type
Length(const math::Matrix<T, N, M> & input)
它工作正常。但是它给我的错误似乎表明它能够确定正确的签名(例如 Length(Matrix<double, 3, 1>&)
(。
如果enable_if
位于参数列表中,为什么编译器找不到匹配的函数,但如果它在返回类型中,则能够找到匹配的函数?
std::enable_if<..., T>::type
是一个嵌套名称,因此无法推断:
参见 [温度扣除类型]/5:
非推导上下文是:
— 使用限定 id 指定的类型的嵌套名称说明符。
。
解决方法是将enable_if
移动到单独的模板参数:
template <typename T, size_t N, size_t M, typename std::enable_if<(M == 1 || N == 1), int>::type = 0>
static inline T Length(
const Matrix<T, N, M> & input)
相关文章:
- 扩展C++生成的代码的模板参数类型名称
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- 将函数参数类型声明为 auto
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 在 C++17 中调用具有不同参数类型的构造函数
- 具有先前参数类型匹配的参数包
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 如何从第一个参数推断第二个参数类型?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 如何用不同的参数类型和数字回调函数
- C++stoi:这两个重载都无法转换所有参数类型
- 为什么std::{container}::template不能推导其参数类型
- 为模板参数类型中的新对象分配内存
- 为指向成员模板参数的指针推导额外模板参数类型的紧凑方式
- 使用std::conditional中的模板来确定函数参数类型
- C++中的短参数类型