模板类型参数“T”,其中“T*”扩展为“nullptr_t”

Template type argument `T` where `T*` expands to `nullptr_t`

我要找的基本上是这样的:

template <typename T> 
struct a { 
    using pointer_type = T*;
};

我想要的是 X 这样 a<X>::pointer_type 的计算结果为 nullptr_t。这可能吗?

编辑:这就是我真正需要的(pointer_type 隐藏在 ENCODERDECODER 模板参数的签名中作为 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 必须在使用解码的 msg 结构的地方接受 nullptr。

std::nullptr_t 不是指针类型。它是一种可以隐式转换为任何指针类型的类型。

你可以专攻:

template <> struct a<std::nullptr_t> { using pointer_type = std::nullptr_t; };

您也可以使用 <type_traits> header 中的 std::conditional:

#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;
};

感谢@Ruks,这就是我想出的:

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;
};