Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将枚举值映射到C++;_C++_C++11_Templates_Enums - Fatal编程技术网

C++ 将枚举值映射到C++;

C++ 将枚举值映射到C++;,c++,c++11,templates,enums,C++,C++11,Templates,Enums,我有一个包含多个成员的枚举类。枚举的目标是在运行时对基本类型(例如int、long、float等)进行编码,以便可以将这些信息存储在数据库中。同时,也存在许多用于处理基元类型的模板化类 问题是:给定一个不是常量的枚举值,我想从这样一个模板类创建一个对象。这是否有可能比在枚举值上创建一个长开关更干净、更具可伸缩性(或者对的答案中建议的映射执行基本相同的操作) 我一直希望模板类型推断可以工作,但它无法编译(可以在这里检查,例如:): #包括 枚举类枚举{ Int, 长的 }; 模板 结构EnumTo

我有一个包含多个成员的枚举类。枚举的目标是在运行时对基本类型(例如int、long、float等)进行编码,以便可以将这些信息存储在数据库中。同时,也存在许多用于处理基元类型的模板化类

问题是:给定一个不是常量的枚举值,我想从这样一个模板类创建一个对象。这是否有可能比在枚举值上创建一个长开关更干净、更具可伸缩性(或者对的答案中建议的映射执行基本相同的操作)

我一直希望模板类型推断可以工作,但它无法编译(可以在这里检查,例如:):

#包括
枚举类枚举{
Int,
长的
};
模板
结构EnumTopPrimitiveType;
模板
结构EnumtPrimitiveType{
使用type=int;
};
模板
结构EnumtPrimitiveType{
使用类型=长;
};
模板
类模板类
{
公众:
TemplatedClass(T init):init{init}{}
void printSize(){std::cout TemplatedClass
^
source_file.cpp:36:6:警告:变量模板仅适用于-std=c++14或-std=gnu++14
自动生成TemplatedClass(T枚举值)->TemplatedClass
^
source_file.cpp:36:38:错误:在“->”标记之前应为“;”
自动生成TemplatedClass(T枚举值)->TemplatedClass
^
source_file.cpp:在函数“int main()”中:
source_file.cpp:44:16:错误:“A”不是“Enum”的成员
枚举值{Enum::A};
^
source_file.cpp:45:34:错误:在“(”标记之前缺少模板参数
auto tmp=makeTemplatedClass(值);
^
我看到的问题:

  • 您不能使用
    template auto-makeTemplateClass(T enumValue)
    ,因为
    T
    不是一个类型。您只需要使用
    template auto-makeTemplateClass()
    并以不同的方式调用函数

  • 您需要使用
    TemplatedClass
    而不仅仅是
    TemplatedClass
    。这是必要的,因为
    type
    是依赖类型

  • 除非是
    const
    constepr
    ,否则不能将
    value
    用作模板参数

  • 以下程序在我的桌面上编译和生成

    #include <iostream>
    
    enum class Enum {
        Int,
        Long
    };
    
    template<Enum T>
    struct EnumToPrimitiveType;
    
    template<>
    struct EnumToPrimitiveType<Enum::Int> {
        using type = int;
    };
    
    template<>
    struct EnumToPrimitiveType<Enum::Long> {
        using type = long;
    };
    
    template<typename T>
    class TemplatedClass
    {
    public:
        TemplatedClass(T init): init{init} {}
        void printSize() { std::cout << sizeof(init) << std::endl; }
    private:
        T init;
    };
    
    template<Enum T>
    auto makeTemplatedClass() -> TemplatedClass<typename EnumToPrimitiveType<T>::type>
    {
        TemplatedClass<typename EnumToPrimitiveType<T>::type> ret(5);
        return ret;
    }
    
    int main()
    {
        Enum const value{Enum::Int};
        auto tmp = makeTemplatedClass<value>();
        tmp.printSize();
    }
    
    #包括
    枚举类枚举{
    Int,
    长的
    };
    模板
    结构EnumTopPrimitiveType;
    模板
    结构EnumtPrimitiveType{
    使用type=int;
    };
    模板
    结构EnumtPrimitiveType{
    使用类型=长;
    };
    模板
    类模板类
    {
    公众:
    TemplatedClass(T init):init{init}{}
    
    void printSize(){std::cout谢谢,您的修复确实可以让它工作,但由于3,它并不是真正的解决方案。不幸的是,一个基本要求是
    value
    不是
    const
    /
    constexpr
    。我的希望是使用
    模板自动生成模板类(T enumValue)
    可以调用此函数,而无需指定
    value
    作为模板参数,以便自动推断,但这似乎不起作用。@misev,既然您知道不能将
    value
    用作模板参数,除非它是
    const
    constepr
    ,您应该转到b回到绘图板上,尝试提出一个解决方案,该解决方案可以处理只有在运行时才能知道的值。我现在无法提出任何建议,因为我不清楚您真正想做什么。
    source_file.cpp:36:27: error: expected ‘)’ before ‘enumValue’
     auto makeTemplatedClass(T enumValue) -> TemplatedClass<EnumToPrimitiveType<T>::type>
                               ^
    source_file.cpp:36:6: warning: variable templates only available with -std=c++14 or -std=gnu++14
     auto makeTemplatedClass(T enumValue) -> TemplatedClass<EnumToPrimitiveType<T>::type>
          ^
    source_file.cpp:36:38: error: expected ‘;’ before ‘->’ token
     auto makeTemplatedClass(T enumValue) -> TemplatedClass<EnumToPrimitiveType<T>::type>
                                          ^
    source_file.cpp: In function ‘int main()’:
    source_file.cpp:44:16: error: ‘A’ is not a member of ‘Enum’
         Enum value{Enum::A};
                    ^
    source_file.cpp:45:34: error: missing template arguments before ‘(’ token
         auto tmp = makeTemplatedClass(value);
                                      ^
    
    #include <iostream>
    
    enum class Enum {
        Int,
        Long
    };
    
    template<Enum T>
    struct EnumToPrimitiveType;
    
    template<>
    struct EnumToPrimitiveType<Enum::Int> {
        using type = int;
    };
    
    template<>
    struct EnumToPrimitiveType<Enum::Long> {
        using type = long;
    };
    
    template<typename T>
    class TemplatedClass
    {
    public:
        TemplatedClass(T init): init{init} {}
        void printSize() { std::cout << sizeof(init) << std::endl; }
    private:
        T init;
    };
    
    template<Enum T>
    auto makeTemplatedClass() -> TemplatedClass<typename EnumToPrimitiveType<T>::type>
    {
        TemplatedClass<typename EnumToPrimitiveType<T>::type> ret(5);
        return ret;
    }
    
    int main()
    {
        Enum const value{Enum::Int};
        auto tmp = makeTemplatedClass<value>();
        tmp.printSize();
    }