枚举的模板参数化 [英] Templates parametrized for enums

查看:525
本文介绍了枚举的模板参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做:

template <enum Type>
class Message {
private:
     Type m_type
};

enum StdInMessages { PrintHello, Echo, ... };
class StdInMsg : Message<StdInMessages>
{ ... }

enum NetworkMessages { DoSomethingElse, Shutdown ... };
class NetworkMsg : Message<NetworkMessages>
{ ... }

当然,实际消息略有不同

Of course, the actual messages are slightly different

为什么不起作用?

template <enum T> class ATemplate {};

我收到此错误

error: use of enum ‘T’ without previous declaration


推荐答案

因为这不是模板的有效语法,除非你正在寻找的是Konrad所回答的

Because that's not valid syntax for a template unless what you're looking for is what Konrad answered.

需要使用 typename class

应该这样做:

enum X
{
   a
};

template <typename T> class ATemplate {};

ATemplate<X> A;

这篇关于枚举的模板参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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