Проверьте наличие функции шаблона в С++C++

Программы на C++. Форум разработчиков
Anonymous
Проверьте наличие функции шаблона в С++

Сообщение Anonymous »

Код: Выделить всё

#include 

template 
struct AllocatorTraits {
private:
template 
static auto malloc(int) -> decltype(static_cast(&U::malloc), std::true_type());

template 
static std::false_type malloc(...);

template 
static auto allocate(int) -> decltype(static_cast(&U::template allocate), std::true_type());

template 
static std::false_type allocate(...);

public:
static constexpr bool has_malloc = decltype(malloc(0))::value;

template 
static constexpr bool has_allocate = decltype(allocate(0))::value;

};

class TestClass {
public:
void* malloc(size_t size) noexcept { return nullptr; }

template 
T* allocate(Args &&...args) noexcept { return nullptr; }
};

static constexpr bool test_malloc = AllocatorTraits::has_malloc;
static constexpr bool test_allocate = AllocatorTraits::has_allocate;
Я использую C++20. Это работает нормально, если я комментирую нешаблоновые функции TestClass, поэтому комментирование TestClass::malloc приведет к тому, что test_malloc будет ложным. Это не скомпилируется, если я закомментирую функцию шаблона

Код: Выделить всё

TestClass::allocate (error C2672: 'AllocatorTraits::allocate': no matching overloaded function found).
Кто-нибудь может помочь мне заставить это работать должным образом, пожалуйста.

Подробнее здесь: https://stackoverflow.com/questions/793 ... ction-in-c

Вернуться в «C++»