C++模板的進階用法中Typelist的用法

來源:互聯網
上載者:User

在Modern C++ Design中提到了很多關於模板的用法,給我印象最深的是Typelist,其他的主要是看不懂,所以不知道有什麼用,所以這裡不談。

但是對於Typelist,書中講述了如何?,但卻沒有用法介紹,我這裡給出一個例子:

#include <iostream>#include <assert.h>#include "typelist.h"using namespace std;struct MsgRegisterRequest{  int container_type;  int  codec_id;  MsgRegisterRequest() :    container_type(0),    codec_id(1)  {}};struct MsgRegisterReply{  bool result;  MsgRegisterReply() :    result(false)  {}  static void echoStatic() { cout << __PRETTY_FUNCTION__ << endl; };  void echo() { cout << __PRETTY_FUNCTION__ << endl; };};struct MsgInviteRequest{  int container_type;  int  codec_id;  MsgInviteRequest() :    container_type(0),    codec_id(1)  {}};struct MsgInviteReply{  bool result;  MsgInviteReply() :    result(false)  {}  static void echoStatic() { cout << __PRETTY_FUNCTION__ << endl; };  void echo() { cout << __PRETTY_FUNCTION__ << endl; };};typedef ly::MakeTypelist< MsgRegisterRequest,                           MsgInviteRequest>::Result MyRequestMessages;typedef ly::MakeTypelist< MsgRegisterReply,                           MsgInviteReply>::Result MyReplyMessages;typedef ly::Append<  MyRequestMessages,  MyReplyMessages  >::Result MyMessage;template<class TList>class MsgQueueServer{public:  explicit MsgQueueServer(const char* name) {}  virtual ~MsgQueueServer() {}  template<typename M>  int postReply(const M& msg)  {    const int id = ly::IndexOf<TList, M>::value;    const int num = ly::Length<TList>::value;    std::cout << "size " << num << " id " << id << endl;    assert(id >= 0);    typedef typename ly::TypeAt<MyMessage, id>::Result MsgType;    MsgType m;    m.echo();    M::echoStatic();    return 0;  }  template<typename M>  void printId(const M& msg)  {    const int id = ly::IndexOf<TList, M>::value;    std::cout << id << std::endl;    return;  }};typedef MsgQueueServer<MyMessage> MyMQServer;class MessageHandlerObject{public:  MessageHandlerObject(MyMQServer& mq):mq_(mq)  {}  void handleRequest(const MsgRegisterRequest& request)   {    MsgRegisterReply reply;    mq_.postReply(reply);  }  void handleRequest(const MsgInviteRequest& request)   {    MsgInviteReply reply;    mq_.postReply(reply);  }  template<typename M>  void printid(const M& msg)  {    mq_.printId(msg);  }private:  MyMQServer& mq_;};int main(){  MyMQServer mq("luoyu");  MessageHandlerObject msgHandler(mq);  MsgRegisterRequest regist;  msgHandler.handleRequest(regist);  MsgInviteRequest invite;  msgHandler.handleRequest(invite);   msgHandler.printid(regist);  msgHandler.printid(invite);  std::cout << "end" << std::endl;  return 0;}/**************** OUTPUT from Console *******************[luo.yu@ly template]$ ./test size 4 id 2void MsgRegisterReply::echo()static void MsgRegisterReply::echoStatic()size 4 id 3void MsgInviteReply::echo()static void MsgInviteReply::echoStatic()01end*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.