相关疑难解决方法(0)

C++为什么SFINAE只有一个类模板参数失败?

我正在使用这个答案的样式SFINAE,以便通过使用适当的成员函数调用泛型矢量对象.例如,以下代码operator[](int) const首先调用,如果不存在,则operator()(int) const:

template<int I> struct rank : rank<I-1> { static_assert(I > 0, ""); };
template<> struct rank<0> {};

template<typename VectorType>
struct VectorWrapper
{
    auto get(int i) const
    {
        return get(v, i, rank<5>());
    }

    template<typename V, typename = std::enable_if_t<has_bracket_operator<const V>::value> >
    auto get(V const& v, int i, rank<2>) const
    {
        return v[i];
    }

    template<typename V, typename = std::enable_if_t<has_parenthesis_operator<const V>::value> >
    auto get(V const& v, int i, rank<1>) const
    {
        return v(i);
    }

    VectorType v; …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++11

7
推荐指数
1
解决办法
847
查看次数

标签 统计

c++ ×1

c++11 ×1

sfinae ×1

templates ×1