我所尋找的基本上是這樣的:
我所尋找的是這樣的:
template <typename T>
結構 a {
using pointer_type = T*;
};
我想要的是這樣的X,以便a<X>::pointer_type評估為nullptr_t。這可能嗎?
編輯:這就是我實際需要的(pointer_type隱藏在ENCODER和DECODER模板引數的簽名中,作為MSG *)
template <int MSGNUM,
typename MSG。
int(& ENCODER)(const MsgHeader* pHeader, const MSG* pMessage, unsigned char* destBuf, int destBufSize)。)
int(& DECODER)(const MsgHeader* pHeader, const unsigned char* msgBody, int msgBodyLen, MSG* decodedMsg)>
struct msgid {
using msg_type = MSG;
static constexpr int msgnum = MSGNUM;
static constexpr auto encoder = ENCODER;
static constexpr auto decoder=DECODER;
};
使用 MSG1 = msgid<1,msg1struct,encodeMsg1,decodeMsg1> 。
使用MSG2 = msgid<2,msg2struct,encodeMsg2,decodeMsg2>。
使用 HDRONLY= msgid<-1,X,encodeHdr,decodeHdr> 。
HDRONLY將不得不接受一個nullptr,其中使用的是解碼后的msg結構。
uj5u.com熱心網友回復:
std::nullptr_t不是一個指標型別。它是一個可以隱式轉換為任何指標型別的型別。
你可以有一個特殊化:
template <> struct a<std: :nullptr_t> { using pointer_type = std::nullptr_t; };
uj5u.com熱心網友回復:
你也可以使用std::conditional從<type_traits>頭:
#include <type_traits>
/ ...
template <typename T>
結構 a {
using pointer_type = typename std::conditional<std::is_same<T, std::nullptr_t>:value。
std::nullptr_t,
T*
>::型別。
};
uj5u.com熱心網友回復:
Thanks,這是我想出的辦法:
template <int MSGNUM,
typename MSG。
int(&ENCODER)(const MsgHeader* pHeader, typename std: :conditional<std::is_same<MSG, std::nullptr_t> :: value, std::nullptr_t, const MSG *>:: type pMessage, unsigned char* destBuf, int destBufSize)。)
int(& DECODER)(const MsgHeader* pHeader, const unsigned char* msgBody, int msgBodyLen, typename std: :conditional<std::is_same<MSG, std::nullptr_t>:value, std::nullptr_t, MSG *>:type decodedMsg)>
struct msgid {
using msg_type = MSG;
static constexpr int msgnum = MSGNUM;
static constexpr auto encoder = ENCODER;
static constexpr auto decoder=DECODER;
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329608.html
標籤:
上一篇:使用指標演算法的最大值二維陣列
