Search code examples
c++c++20c++-conceptsc++-templates

Can I write a concept to test for the existence of a "templated member"


  1. Is it possible to write a concept to check for the existence of a member that is a template (without just trying an arbitrary specialization)?

For example, check if the type contains a function zug<T>(T) taking a template parameter.

struct SNoZug {};

struct SZug
  {
    template <typename T> // (A)
    void zug(T) {};
  };

template <typename T>
concept has_zug = ???; // << CAN THIS BE DONE?

static_assert(!has_zug<SNoZug>);
static_assert(has_zug<SZug>);
  1. Can this be done if (A) uses a concept parameter (eg: template <some_other_concept T>)?
  2. Can this be done if zug (A) uses a variadic parameter?
  3. Can this be done if zug uses a value parameter?
  4. Can this be done if zug uses an auto parameter?
  5. Can this be done if zug uses a template template parameter?
  6. Can this be done if zug was a member class instead of a member function?

NOTE: Related unanswered question: Check the existence of a member function template in a concept definition.

NOTE: There's some proposed code for a potentially related problem here, but I'm not sure if it's valid C++: C++20 Template Template Concept Syntax.


Solution

  • You can check for .zug<int>(0) being valid, or any fixed instantiation (including those that are functions of the other template arguments).

    This will work the same as testing for .zug() would.

    You cannot check for a generic template without trying to instantiate.

    It may be possible after reflection is added to C++ to do so.