Проверьте, есть ли в классе возможно перегруженный оператор вызова функции

Мне интересно, можно ли реализовать черту в C++20 чтобы проверить, есть ли тип T таков, что он имеет возможно перегруженный / возможно шаблонный оператор вызова функции: operator().

// Declaration
template <class T>
struct has_function_call_operator;

// Definition
???  

// Variable template
template <class T>
inline constexpr bool has_function_call_operator_v 
= has_function_call_operator<T>::value;

чтобы такой код привел к правильному результату:

#include <iostream>
#include <type_traits>

struct no_function_call_operator {
};

struct one_function_call_operator {
    constexpr void operator()(int) noexcept;
};

struct overloaded_function_call_operator {
    constexpr void operator()(int) noexcept;
    constexpr void operator()(double) noexcept;
    constexpr void operator()(int, double) noexcept;
};

struct templated_function_call_operator {
    template <class... Args>
    constexpr void operator()(Args&&...) noexcept;
};

struct mixed_function_call_operator
: overloaded_function_call_operator
, templated_function_call_operator {
};

template <class T>
struct has_function_call_operator: std::false_type {};

template <class T>
requires std::is_member_function_pointer_v<decltype(&T::operator())>
struct has_function_call_operator<T>: std::true_type {};

template <class T>
inline constexpr bool has_function_call_operator_v 
= has_function_call_operator<T>::value;

int main(int argc, char* argv[]) {
    std::cout << has_function_call_operator_v<no_function_call_operator>;
    std::cout << has_function_call_operator_v<one_function_call_operator>;
    std::cout << has_function_call_operator_v<overloaded_function_call_operator>;
    std::cout << has_function_call_operator_v<templated_function_call_operator>;
    std::cout << has_function_call_operator_v<mixed_function_call_operator>;
    std::cout << std::endl;
}

В настоящее время он печатает 01000 вместо того 01111. Если это невозможно в самом широком смысле, можно предположить, чтоTнаследуется, если это помогает. Приветствуются самые странные из возможных приемов метапрограммирования шаблонов, если они полностью соответствуютC++20 стандарт.

1 ответ

Решение

&T::operator() неоднозначно для трех неудачных случаев.

Итак, ваши черты найдены, есть однозначное operator()

Поскольку вы позволяете Т не быть final, мы можем применить ваши черты к (поддельному) классу с существующими унаследованными operator() и класс для проверки:

template <class T>
struct has_one_function_call_operator: std::false_type {};

template <class T>
requires std::is_member_function_pointer_v<decltype(&T::operator())>
struct has_one_function_call_operator<T>: std::true_type {};

struct WithOp
{
    void operator()() const;  
};

template <typename T>
struct Mixin : T, WithOp {};

// if T has no `operator()`, Mixin<T> has unambiguous `operator()` coming from `WithOp`
// else Mixin<T> has ambiguous `operator()`
template <class T>
using has_function_call_operator =
    std::bool_constant<!has_one_function_call_operator<Mixin<T>>::value>;

template <class T>
inline constexpr bool has_function_call_operator_v 
= has_function_call_operator<T>::value;

Демо

Другие вопросы по тегам