c++ Как узнать, объявлен ли в классе using value_type?

template <class Ty>
class A{
  public:
  using value_type = Ty;
  using pointer = Ty*; 
};

int main(){
  static_assert(has_using<A, value_type>, "!");
  static_assert(has_using<A, pointer>, "!");
}

Как реализовать что-то подобное, чтобы можно было проверить у любого класса наличие любого using?


Ответы (1 шт):

Автор решения: user7860670

В этом примере специализация, наследующая от true_type будет выбираться при наличии в типе-параметре шаблона типа value_type как более специализированная специализация.

#include <type_traits>

template<typename x_Class, typename x_Enabled = void>
class
has_value_type final
: public ::std::false_type
{};

template<typename x_Class>
class
has_value_type<x_Class, ::std::void_t<typename x_Class::value_type>> final
: public ::std::true_type
{};

template <class Ty>
class A{
  public:
  using value_type = Ty;
  using pointer = Ty*; 
};

static_assert(has_value_type<A<int>>::value);

https://godbolt.org/z/rdGvnn

→ Ссылка