在模板中的无名枚举 [英] Nameless enums in templates

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

问题描述

各种模板代码如下所示:

  template< typename T& 
class foo
{
enum {value =< some expr with T> };
};

例如可以看到这里在主要检查程序中,我也看到了一个因子实现。 / p>

我的问题是为什么使用无名枚举?这有什么特别的原因吗?
例如,静态const int也可以工作。



编辑:



<感谢链接,它提供了我正在寻找的洞察!

解决方案

静态const变量会占用内存说),而枚举不占用任何内存。它们只存在于编译器的世界中。在运行时它们只是正整数。



除了它会工作,除非编译器错误地执行标准。



有关此主题的详细主题在boost邮件 - 列表:


Alot templated code looks like this:

template <typename T>
class foo
{
   enum { value = <some expr with T> };
};

An example can be seen here in the prime check program and I've seen it in a Factorial implementation once too.

My question is why use a nameless enum? Is there a particular reason to this? A static const int could work as well for example?

edit:

@Benoît: Thanks for the link, it provided the insight I was looking for!

解决方案

A static const variable would take up memory (like Sean said), whereas enums do not take any memory. They only exist in the compiler's world. At runtime they are just regular integers.

Other than that it would work, except for bad implementation of the standard by the compiler.

There is a thorough thread on the subject in boost mailing-list :

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

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