假設我有以下課程:
/* Frame.hpp */
template<class PayloadType>
class Frame {
int address;
PayloadType payload;
}
/* RequestPacket.hpp */
template<class PayloadType>
class RequestPacket { // Also a similar ResponsePacket exists
Command command; // Command is an enum
PayloadType payload;
}
/* GetMeanTemperatureRequest.hpp */
class GetMeanTemperatureRequest { // Many different Requests and Responses
Period period; // Again, Period is an enum
}
我有以下功能:
/* Serializer.hpp */
template<class From, To>
size_t Serializer::serializeTo(const From& input, To buffer);
然后我想實體化這個函式,比如說 Frame:
/* Frame.cpp */
#include "Serializer.hpp"
template<class PayloadType> // These 2 lines are the
template<> // subject of my question
size_t Serializer::serializeTo(const Frame<PayloadType>& input, uint8_t* buffer) {
// implementation
}
然后我會得到error: too many template-parameter-lists。
如果我洗掉模板<>,那么我會得到error: prototype for std::size_t Serializer::serializeTo(const Frame<PayloadType>&, uint8_t*) does not match any in class Serializer
如果我改變template<...>周圍的兩個 s 也無濟于事
編譯器想要什么?
uj5u.com熱心網友回復:
專業化需要與主要宣告相匹配。在您的主要宣告中,您有:
template<class From, To>
size_t Serializer::serializeTo(const From& input, To buffer);
所以你的專業化需要是
template<>
size_t Serializer::serializeTo(const concrete_from_type& input, concrete_to_type buffer);
您正在做的是嘗試將專業化與另一個模板組合以嘗試獲得部分專業化,但這是不允許的。您可以做的只是添加另一個函式多載,例如
template<class PayloadType, To>
size_t Serializer::serializeTo(const Frame<PayloadType>& input, To buffer);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/530634.html
標籤:C c 11模板仿制药
上一篇:C# GDI+ 畫心形 跳動影片
