【问题标题】:SFINAE works as return type, but not parameter typeSFINAE 作为返回类型,但不是参数类型
【发布时间】:2018-01-10 13:26:47
【问题描述】:

我试图使用enable_if 来避免重复代码。只要放在返回类型中,它就可以正常工作,但如果它在参数中,则不会。在此作为this 的副本关闭之前,我得到的错误不是重新定义,而是“没有匹配的调用函数”。这是我使用 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&lt;double, 3, 1&gt;&amp;))。

如果enable_if在参数列表中,为什么编译器无法找到匹配的函数,但如果它在返回类型中却能找到?

【问题讨论】:

  • @MassimilianoJanes 为什么ints 不可推断?基于这个错误,我认为编译器推断得很好。
  • int 是可演绎的,T 不是,因为它出现在非演绎的上下文中(即 std::enable_if::type)
  • 这是你想要的吗? ideone.com/2h0ZHl
  • @KillzoneKid 是的,这也很有效(template &lt;typename T, size_t N, size_t M, typename Dummy = typename std::enable_if&lt;(M == 1 || N == 1), T&gt;::type&gt; 也是如此)。但是,如果括号中的示例有效,我不明白为什么它在参数列表中无效。在这两种情况下,编译器都必须推断出T 的类型。

标签: c++ templates sfinae enable-if


【解决方案1】:

std::enable_if&lt;..., T&gt;::type 是嵌套名称,因此无法推断:

[temp.deduct.type]/5:

未推断的上下文是:

—— 使用 qualified-id 指定的类型的 nested-name-specifier

。 . .

作为一种解决方法,将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)

【讨论】:

  • 我现在会避免void* 的东西,它是否合法还有待商榷。这里某处有一个缺陷报告。最好将其设为int = 0 或其他内容。编辑:这里:cplusplus.github.io/EWG/ewg-active.html#175
  • 链接是当前草稿,是吗?例如,它是否不同? c++11?
  • @AndyG - 已更新,谢谢。 AviGinsburg - 据我所知,这条基本规则一直存在。
  • 换句话说,任何包含 :: 的东西都不能被推导出来。 除非 :: 是指向成员声明的一部分;)
猜你喜欢
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 2018-11-21
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多