Template specialization: C++ templates that only accept certain types (revisited)

删除回忆录丶 提交于 2019-12-11 06:14:41

问题


I want to make a simple message template class as follows. I followed the structure verbatim from the chosen answer posted here. However, this seems to break in Visual Studio 2013.

template<typename T> Message;

template<> Message <std::vector<uint8_t>>
{
public:

};

template<> Message <std::string>
{

};

IntelliSense tells me that Message is not a template.

Placing class before Message in the forward declaration results in a slightly better, but equally annoying IntelliSense error: expected and identifierafter the opening brace { of each template.

template<typename T> class Message;

template<> Message <std::vector<uint8_t>>
{
public:

};

template<> Message <std::string>
{

};

I should note that all the above code is currently being placed in a header file.


回答1:


You need to include the class keyword in the template specialization as well. I don't know why the linked answer doesn't do that.

template<typename T> class Message;

template<> class Message <std::vector<uint8_t>>
{
public:

};

template<> class Message <std::string>
{

};


来源:https://stackoverflow.com/questions/38107797/template-specialization-c-templates-that-only-accept-certain-types-revisited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!