具有特定接口的类型的 C++ 模板特化

C++ template specialization with types which have certain interface

假设我有这样的模板:

template<class T>
class A
{
   ...
};

我希望这个模板只有在将替代 T 的类型具有特定接口时才能专门化。比如这个类型必须有这样两个方法:

int send(const char* buffer, size_t size);
int receive(char* buffer, size_t size);

如何对模板进行限制? 感谢您的帮助!

更新:

这个问题是关于SFINAE的?与继承或 class 设计无关。

最简单的方法是在 A 中使用 T::sendT::receive,任何不实现这些的类型都将导致编译时无法实例化模板。你只需要 SFINAE 来区分模板的专业化。

例如

template<class T>
class A
{
    void useT(T & theT)
    {
        char buf[20]
        theT.send("Some Thing", 11);
        theT.recieve(buf, 20);
    }
};

另一个答案显然更可取,但由于您明确要求 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!
}

检查class是否有带有特定签名的方法是SFINAE原理的常见应用。例如,您可以检查 there and there.