Calling a free function instead of a method if it doesn't exist

前端 未结 4 817
滥情空心
滥情空心 2021-02-01 07:33

Suppose you have a family of type-unrelated classes implementing a common concept by means of a given method returning a value:

class A { public: int val() const         


        
4条回答
  •  悲哀的现实
    2021-02-01 07:43

    You can use return-type SFINAE:

    template
    auto val_of(const T& t) -> decltype(std::declval().val())
    {
        return t.val();
    }
    
    int val_of(...)
    {
        return 0;
    }
    

提交回复
热议问题