C++ why does SFINAE fail with only a class template parameter?

前端 未结 3 1323
鱼传尺愫
鱼传尺愫 2021-01-18 03:14

I\'m using SFINAE in the style of this answer in order to call a generic vector object by using an appropriate member function. For example, the following code calls o

3条回答
  •  猫巷女王i
    2021-01-18 04:03

    Or you can just use tag-dispatching:

    auto get(int i) const
    {
        return get(i, has_bracket_operator(), has_parenthesis_operator());
    }
    
    auto get(int i, std::true_type /*brackets*/, std::false_type /*parenthesis*/) const
    {
        return v[i];
    }
    
    auto get(int i, std::false_type /*brackets*/, std::true_type /*parenthesis*/) const
    {
        return v(i);
    }
    

    demo

提交回复
热议问题