模板专门化:只接受某些类型(重访)的C ++模板 [英] Template specialization: C++ templates that only accept certain types (revisited)

查看:279
本文介绍了模板专门化:只接受某些类型(重访)的C ++模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的消息模板类如下。我从此处张贴的选择答案逐字跟踪结构。但是,这似乎在Visual Studio 2013中断。

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告诉我 Message不是模板

IntelliSense tells me that Message is not a template.

之前放置 class 声明导致稍微更好,但同样令人讨厌的IntelliSense错误:预期和标识符后每个模板的开始大括号 {

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.

推荐答案

您还需要在模板专业化中包含关键字。我不知道为什么链接的答案不这样做。

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>
{

};

这篇关于模板专门化:只接受某些类型(重访)的C ++模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆