其他答案顯然是首選的,但因爲你明確地要求SFINAE,在這裏你去:
#include <iostream>
#include <utility>
// std::void_t in C++17
template <typename...>
using void_t = void;
// Check if there is a member function "send" with the signature
// int send(const char*, size_t)
template < typename T >
using send_call_t = decltype(std::declval<T>().send(std::declval<char const *>(), std::declval<size_t>()));
template < typename, typename = void_t<> >
struct is_send_callable : std::false_type {};
template < typename T >
struct is_send_callable< T, void_t< send_call_t<T> > > : std::is_same< send_call_t<T>, int > {};
// Check if there is a member function "receive" with the signature
// int receive(const char*, size_t)
template < typename T >
using recv_call_t = decltype(std::declval<T>().receive(std::declval<char *>(), std::declval<size_t>()));
template < typename, typename = void_t<> >
struct is_recv_callable : std::false_type {};
template < typename T >
struct is_recv_callable< T, void_t< recv_call_t<T> > > : std::is_same< recv_call_t<T>, int > {};
// Make a struct which implements both
struct sndrecv
{
int send(const char* buffer, size_t size)
{
std::cout << "Send: " << buffer << ' ' << size << '\n';
return 0;
}
int receive(char* buffer, size_t size)
{
std::cout << "Receive: " << buffer << ' ' << size << '\n';
return 0;
}
};
// Disable A if T does not have send and receive
template < typename T, typename >
class A;
template < typename T, typename = typename std::enable_if< is_send_callable<T>::value && is_recv_callable<T>::value >::type >
class A {};
int main() {
A<sndrecv> a;
//A<int> b; // BOOM!
}
不要將模板與抽象基類要求混淆。如果您需要一組特定的功能,請將該接口定義爲一個類,並將其作爲一個簡單的類型化需求。 – tadman
可能重複[檢查類是否具有簽名功能](https://stackoverflow.com/questions/24975147/check-if-class-has-function-with-signature) –
@tadman,假設您可以添加基類到您感興趣的所有類型。 – Caleth