Чудя се дали би било възможно да се приложи характеристика в 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
.
&T::operator()
е двусмислено за 3-те неуспешни случая. - person Jarod42   schedule 16.06.2020