模板類,模板成員函式,模板巢狀類
阿新 • • 發佈:2018-12-21
在模板類中宣告一個模板成員函式,定義方式:
.h檔案
template <typename T, std::size_t N>
struct MsgOperaTrait : MsgOperaBase
{
enum {MSGID = N};
template<class K>
void NotifyCardOperFunc(K* pCardOper, LONG tokenExcept, int nNtfMsg = 0);
};
.cpp檔案
template<class T, std::size_t N> template<class K> void MsgOperaTrait<T, N>::NotifyCardOperFunc(K* pCardOper, LONG tokenExcept, int nNtfMsg) { }
在上面組合中如果需要對外提過一個介面,內部通過結構體區分呼叫不同的實現:
.h檔案
//初等模板 #define MSG_OPERTRAIT_NOTIFY_CARD_OPER_BASE() \ template<class M, class Enable = void>\ struct NotifyCardOper\ {\ void operator()(M* pCardOper, LONG tokenExcept, CGameTable* pTable, CGameServer* pMainServer, int nNtfMsg = 0){}\ }; //部分特化模板 #define MSG_OPERTRAIT_NOTIFY_CARD_OPER(class_name) \ template<class M> \ struct NotifyCardOper<M, typename std::enable_if<std::is_same<M, class_name>::value>::type>\ {\ void operator()(M* pCardOper, LONG tokenExcept, CGameTable* pTable, CGameServer* pMainServer, int nNtfMsg = 0); \ }; template <typename T, std::size_t N> struct MsgOperaTrait : MsgOperaBase { enum {MSGID = N}; template<class K> void NotifyCardOperFunc(K* pCardOper, LONG tokenExcept, int nNtfMsg = 0); ////註冊notifyCard*通知事件 MSG_OPERTRAIT_NOTIFY_CARD_OPER_BASE() MSG_OPERTRAIT_NOTIFY_CARD_OPER(CATCH_CARD); MSG_OPERTRAIT_NOTIFY_CARD_OPER(CHI_CARD); MSG_OPERTRAIT_NOTIFY_CARD_OPER(CHANGE_CARD); };
.cpp檔案
#define MSG_OPERA_NTF_CLASS_FUNC_DECLARE(class_name)\ template<class T, std::size_t N>\ template<class M>\ void MsgOperaTrait<T, N>::NotifyCardOper<M, typename std::enable_if<std::is_same<M, class_name>::value>::type>::operator()(M* pCardOper, LONG tokenExcept, CGameTable* pTable, CGameServer* pMainServer, int nNtfMsg)\ template<class T, std::size_t N> template<class K> void MsgOperaTrait<T, N>::NotifyCardOperFunc(K* pCardOper, LONG tokenExcept, int nNtfMsg) //對外提供這個介面 { NotifyCardOper<K> oper; oper(pCardOper, tokenExcept, pTable, pMainServer, nNtfMsg); } MSG_OPERA_NTF_CLASS_FUNC_DECLARE(CATCH_CARD) { std::cout<<"caught"<<std::endl; } MSG_OPERA_NTF_CLASS_FUNC_DECLARE(CHI_CARD) { std::cout<<"chi"<<std::endl; } MSG_OPERA_NTF_CLASS_FUNC_DECLARE(CHANGE_CARD) { std::cout<<"change"<<std::endl; }
呼叫:
CATCH_CARD caughtCard;
NotifyCardOperFunc(&caughtCard, 0); //輸出 caught
CHI_CARD chiCard;
NotifyCardOperFunc(&chiCard, 0); //輸出 chi
CHANGE_CARD changeCard;
NotifyCardOperFunc(&changeCard, 0); //輸出 change
參考std::enable_if提供的例項
#include <type_traits>
#include <iostream>
#include <string>
namespace detail { struct inplace_t{}; }
void* operator new(std::size_t, void* p, detail::inplace_t){
return p;
}
// #1 ,通過返回型別使用
template<class T, class... Args>
typename std::enable_if<std::is_trivially_constructible<T, Args&&...>::value>::type
construct(T* t, Args&&... args)
{
std::cout << "constructing trivially constructible T\n";
}
// #2
template<class T, class... Args>
std::enable_if_t<!std::is_trivially_constructible<T, Args&&...>::value> // 使用幫助型別
construct(T* t, Args&&... args)
{
std::cout << "constructing non-trivially constructible T\n";
new(t, detail::inplace_t{}) T(args...);
}
// #3 ,通過形參啟用
template<class T>
void destroy(T* t,
typename std::enable_if<std::is_trivially_destructible<T>::value>::type* = 0)
{
std::cout << "destroying trivially destructible T\n";
}
// #4 ,通過模板形參啟用
template<class T, typename std::enable_if<!std::is_trivially_destructible<T>::value && (std::is_class<T>::value || std::is_union<T>::value), int>::type = 0>
void destroy(T* t)
{
std::cout << "destroying non-trivially destructible T\n";
t->~T();
}
// #5 ,通過模板形參啟用
template<class T,
typename = std::enable_if_t<std::is_array<T>::value> >
void destroy(T* t) // 注意,不修改函式簽名
{
for (std::size_t i = 0; i < std::extent<T>::value; ++i) {
destroy((*t)[i]);
}
}
/*
template<class T,
typename = std::enable_if_t<std::is_void<T>::value> >
void destroy(T* t){} // 錯誤:與 #5 擁有相同簽名
*/
// A 的部分特化通過模板形參啟用
template<class T, class Enable = void>
class A {}; // 初等模板
template<class T>
class A<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
}; // 為浮點型別特化
//template void func<X>(); // #2
int main()
{
std::aligned_union_t<0, int, std::string> u;
construct(reinterpret_cast<int*>(&u));
destroy(reinterpret_cast<int*>(&u));
construct(reinterpret_cast<std::string*>(&u), "Hello");
destroy(reinterpret_cast<std::string*>(&u));
A<int> a1; // OK ,匹配初等模板
A<double> a2; // OK ,匹配部分特化
system("pause");
return 0;
//func<X, X::value1>();
}