在C ++中将枚举用作模板类型参数 [英] Using enum as template type argument in C++

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

问题描述

在C ++中使用枚举作为模板(类型)参数是否有任何限制/问题?

are there any restrictions / problems using an enum as template (type) argument in C++?

示例:

enum MyEnum
{
    A, B, C, D, E
};

template <typename _t>
class MyTemplate
{
public:
   _t value;

   void func(const _t& param) { /* .... */ }
};

// ....

MyTemplate<MyEnum> MyInstance;


我在Win32/x86上通过VS 2008(SP1)使用MSVC ++的实际问题是与使用枚举作为模板参数的类相关联的多个编译错误(=编译器报告的错误).不幸的是,由于我的项目变得有点复杂(您可以将其视为设计错误:P),引发这些错误的模板类是派生,嵌套的,甚至专门用于带有枚举模板参数的类.


My actual problem using MSVC++ via VS 2008 (SP1) on Win32/x86 are several compilation errors (= errors reported by the compiler) in association with classes using enums as template arguments. As my project unfortunately has become a bit complex (you can consider that as a design error :P), the template classes raising these errors are derived, nested and even specialised on a class with enum template parameter.

尝试构建时,编译器在仅有注释的行中报告了许多错误/无用的错误,例如"C2059:语法错误:'public'".我可以通过替换与示例中的const _t&示例相似的方法来解决其中的许多问题.用_t进行参数设置(即复制参数),但是我既无法解决所有这些错误,也无法知道为什么这种帮助". **我知道,上面的简单示例编译时没有错误.

Trying to build, the compiler reports many wrong/useless errors such as "C2059: syntax error: 'public'" in lines where there is only a comment. Many of them I could fix by replacing in methods similar to the one in the example the const _t& param by _t (i.e. copying the parameter), but neither could I fix all of these errors nor do I have a clue why this "helps". **I know, the simple example above compiles w/o errors.

使用int而不是enum,我的项目编译没有错误.

Using int instead of enum, my project compiles w/o errors.

在此先感谢您的提示或提示!

Thanks in advance for any hint or tip!

修改:

毕竟,我认真地将其视为编译器错误.当我尝试使用简化的代码重现错误时,我仅在所有构建"中的50%处得到了它们,而不是确定性很高的:
例如.尝试编译,并报告了这些错误.重建-不变.删除评论,建立-不变.重建-然后:没有错误,可以正常编译.

After all, I seriously consider this as a compiler bug. When I tried to reproduce the errors with simplified code, I got them only in 50 % of all "builds", not very deterministic:
E.g. tried to compile, and it reported these errors. Rebuild - no change. Deleted a comment, build - no change. Rebuild - and then: no errors, compiles fine.

我已经遇到了一些编译器错误(我想在2万行代码中有2或3个错误),但是在我看来,这很奇怪.
关于如何确定编译器的任何建议?

I've already met a few compiler bugs (2 or 3 I guess within 20k lines of code), but this one seems to me very strange.
Any suggestions how to figure out if it is the compiler?

推荐答案

参考原始问题:

在C ++中使用枚举作为模板(类型)参数是否有任何限制/问题?

are there any restrictions / problems using an enum as template (type) argument in C++?

我没有找到任何东西-我也不认为有任何东西.事实证明这不是一个好主意,因为这种技术的使用频率不高,因此可能会有一些(更多)与此相关的编译器错误,正如Potatoswatter所说.
考虑以下示例:

I didn't find any - and I don't think there are any. It might turn out to be a bad idea because this technique it is not used that often, so there might be a few (more) compiler bugs relating to this, just as Potatoswatter said.
Consider the following example:

enum MyEnum : int
{
    A, B, C, D
};

template <typename _t> class MyTemplate
{
public:
    void print()
    {
        cout << "not using any specialisation" << endl;
    }
};
    template <> class MyTemplate <MyEnum>
    {
    public:
        void print()
        {
            cout << "MyEnum specialisation" << endl;
        }
    };
    template<> class MyTemplate <int>
    {
    public:
        void print()
        {
            cout << "int specialisation" << endl;
        }
    };

template <typename _t> void print(_t param)
{
    MyTemplate<_t> m;
    m.print();
}


int main()
{
    print(A);
    print(5);

    return 0;
}

输出为:

MyEnum专业化
国际专业化

MyEnum specialisation
int specialisation

对于这些简单的示例,一切正常且符合预期,并且枚举与任何其他类型的模板类型参数完全一样(=我看不出任何问题的原因).

最初,我在问题中介绍了该示例以说明该问题的含义(枚举作为模板类型参数,将可能的用法显示为成员或方法参数类型,依此类推).为了提供一些背景知识,即为什么,我问了这个问题(假设我问"int是否有任何问题"),我提到了编译我的实际项目时遇到的这些奇怪的问题.
抱歉,我无法提取完整的代码段并重现错误,至少我能得到的是将2k行代码分成4个文件,其中有语法错误:'public'"和其他一些错误编译项目时出现语法错误,并且在某些情况下(在删除注释或重建时(=删除中间文件))它们会出现/消失.不幸的是,重建对原始项目无济于事,在原始项目中,我不得不将特殊化从枚举类型替换为int.

For these simple examples, everything works fine and as expected and the enum works perfectly as any other type as template type argument (= I don't see any reason for problems).

Originally, I introduced the example in the question to show what I meant with that question (enum as template type argument, show possible usages as member or method argument type and so on). To provide a bit of background, i.e. why I asked that question (imagine I asked "are there any problems with int"), I mentioned these strange problems compiling my actual project.
I'm sorry I could not extract a snippet of it that is complete in itself and reproducing the errors, the least I could get were 2k lines of code splitted into 4 files, where a "syntax error : 'public'" and some other syntax error were raised when I compiled the project, and they appeared / disappeared under certain circumstances, when deleting a comment or re-building (= deleting the intermediate files). Unfortunately, rebuilding does not help with the original project, where I had to replace a specialisation from an enum type to int.

因此,感谢大家的提示和技巧.在我看来,潜在的问题似乎是编译器错误,这使问题变得毫无意义,因为答案似乎仅仅是不-使用枚举作为模板类型参数没有任何限制" .不便之处,敬请谅解.

So, thanks everyone for your hints and tips. The underlying problem seems to me to be a compiler bug, what makes the question a bit pointless, as the answer seems to be just "no - there are no restrictions using an enum as template type argument". Sorry for the inconvenience.

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

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