Aisec Nory
Aisec Nory

Reputation: 425

Is it possible to determine if class has template method of certain signature

I know that it is possible to determine if class has certain member with SFINAE. But is there a way to determine if class has certain template method? For example:

class Foo {
public:
    template<typename T>
    int method(std::vector<T> vec);
};

I also know that you can detect particular instantiations of member template (i.e. for specified T). But I wonder if it possible at all to do so with arbitrary T?

EDIT:

To be more precise why I need T to be arbitrary type consider example:

class Foo {
public:
    template<typename Derived>
    void func(Eigen::ArrayBase<Derived> &&arr)
    {
        // implementation 
    }
};

using Matrix = Eigen::Array<double, -1, -1, Eigen::RowMajor>;

Matrix m1(10, 10);

Foo foo;

foo.func(m1.row(1));
foo.func(m1.col(1));
foo.func(m1.row(2).segment(2, 4));

func can be called with any of those expressions, but Derived inside those calls will be different:

  1. Eigen::Block<Eigen::Array<double, -1, -1, 1>, 1, -1, true>
  2. Eigen::Block<Eigen::Array<double, -1, -1, 1>, -1, 1, false>
  3. Eigen::Block<Eigen::Block<Eigen::Array<double, -1, -1, 1>, 1, -1, true>, 1, -1, false>

Upvotes: 2

Views: 108

Answers (1)

Tates
Tates

Reputation: 89

In C++20 this may be done with constraints/concepts I reckon. https://en.cppreference.com/w/cpp/language/constraints See if something like this works:

template<typename T1, typename T2>
concept Methodable = requires (T1 x) {
    std::vector<T2> v; x.method<T2>(v);
};

I didn't test this out, but this may work just fine. I don't have a compiler handy on my current device to test something like this out.

Upvotes: 3

Related Questions