What I'm looking for is basically this:
template <typename T>
struct a {
using pointer_type = T*;
};
What I want is such X
so that a<X>::pointer_type
evaluates to nullptr_t
. Is this possible?
Edit: This is what I actually need (the pointer_type
is hidden in the signatures of the ENCODER
and DECODER
template arguments as 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;
};
using MSG1 = msgid<1,msg1struct,encodeMsg1,decodeMsg1>;
using MSG2 = msgid<2,msg2struct,encodeMsg2,decodeMsg2>;
using HDRONLY= msgid<-1,X,encodeHdr,decodeHdr>;
HDRONLY would have to accept a nullptr where the decoded msg structure is used.
CodePudding user response:
std::nullptr_t
is not a pointer type. It's a type with an implicit conversion to any pointer type.
You could have a specialisation:
template <> struct a<std::nullptr_t> { using pointer_type = std::nullptr_t; };
CodePudding user response:
You could also use std::conditional
from the <type_traits>
header:
#include <type_traits>
// ...
template <typename T>
struct a {
using pointer_type = typename std::conditional<std::is_same<T, std::nullptr_t>::value,
std::nullptr_t,
T*
>::type;
};
CodePudding user response:
Thanks to @Ruks this is what I came up with:
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;
};