Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_Enums_Return Type - Fatal编程技术网

C++ 如何使用c+;中的映射实现模板的返回类型查找+;?

C++ 如何使用c+;中的映射实现模板的返回类型查找+;?,c++,templates,enums,return-type,C++,Templates,Enums,Return Type,我有两个数组。包含指向基类的指针的指针。另一个包含枚举,该枚举的值提供指向实类型的指针 我现在想要一个函数,它可以为我获取一个数组,其中包含与它们关联的特定枚举值的条目,并以正确的返回类型返回该数组 示例(半伪代码): std::数组a={TypeAInstance,TypeBInstance}; 数组b={TypeA,TypeB}; 模板 std::数组GetEntriesOfType() { std::数组ret; 对于(int i=0;i

我有两个数组。包含指向基类的指针的指针。另一个包含枚举,该枚举的值提供指向实类型的指针

我现在想要一个函数,它可以为我获取一个数组,其中包含与它们关联的特定枚举值的条目,并以正确的返回类型返回该数组

示例(半伪代码):

std::数组a={TypeAInstance,TypeBInstance};
数组b={TypeA,TypeB};
模板
std::数组GetEntriesOfType()
{
std::数组ret;
对于(int i=0;i<2;i++)
{
ret[i]=nullptr;
如果(b[i]==EnumForType(SearchType))ret[i]=a[i];
}
}
但是,我不知道如何构造EnumForType函数或reverse TypeForEnum函数,该函数允许我使用以下方法声明模板返回类型:

template<SearchTypeEnum>
TypeForEnum(SearchTypeEnum) GetEntriesOfType();
模板
TypeForEnum(SearchTypeEnum)GetEntriesOfType();
如果可能的话,我想定义这样一个constexpr函数

潜在的问题是,我有一系列的插件,我会迭代。每个插件需要根据类型进行不同的处理。但是,由于该类型依赖于运行时,并且我不能进行动态分配(嵌入式系统约束),所以我将所有插件存储在固定的预保留内存空间中。我需要在运行时以某种方式将指向基类的指针转换为正确的类型,并相应地进行处理


这个GetEntriesOfType函数应该会使它更方便。当然,我可以获取这两个数组,然后在对这两个数组进行迭代时执行switch语句,但我想对该库的最终用户进行简化。

一种方法是将每个类类型映射到一个枚举器值:

class A;
class B;

enum class Types {
    A,
    B
};

template<class T> struct EnumForType;

template<> struct EnumForType<A> { static constexpr Types value = Types::A; };
template<> struct EnumForType<B> { static constexpr Types value = Types::B; };

int main() {
    auto a = EnumForType<A>::value;
    auto b = EnumForType<B>::value;
}
A类;
乙级;;
枚举类类型{
A.
B
};
模板结构EnumForType;
模板结构EnumForType{static constexpr Types value=Types::A;};
模板结构EnumForType{static constexpr Types value=Types::B;};
int main(){
自动a=EnumForType::value;
自动b=EnumForType::value;
}
你会像这样使用它:

if(b[i] == EnumForType<SearchType>::value)
if(b[i]==EnumForType::value)

您可以为每种类型创建一个id,而不是枚举

让我们为带有模板的类型定义此id。因为您更喜欢constexpr,所以RTTI不是一个选项。以下是我需要类型id时所做的操作:

template<typename>
void type_id() {}

using type_id_t = void(*)();
我通常避免将类型与枚举相关联,因为枚举并不表示类型,而是映射到简单的整数值


另外,如果您的数组是constexpr,您甚至可以返回一个只适合
SearchType
的所有实例的大小的数组,而不是用
nullptr

填充其他值。将枚举(或整数)映射到类型的核心要素是某些内容的完全专门化。例如,你可以

template <TypeEnum> struct EnumToType;
template <> struct EnumToType<TypeA> { using type = TypeAType; };
template <> struct EnumToType<TypeB> { using type = TypeBType; };
// ...

你想解决的实际问题是什么?你不能通过传统的多态性来解决它吗?使用类型枚举和if或switch语句通常是糟糕设计的标志。@JoachimPileborg添加了更多信息,如果您知道如何摆脱这种设计,请随意。我以前尝试过更改它,但效果并不理想。数组本身不是constexpr。该数组具有与运行时相关的类型。但是,因为我想使用函数来确定模板的返回类型,所以除了constexpr之外,它不能有任何其他内容,这就是我最初的意思。你的方法的缺点是,我理解的方式是,当我在多个上下文中使用我的TypeEnum时,这些TypeID与基础类型紧密绑定。我不理解你所说的“那些TypeID与基础类型紧密绑定”是什么意思,这是否意味着你想要一个定义了所有类型的列表,就像枚举一样?或者您想在不知道要使用的类型的情况下使用type_id值?或者您想循环所有类型的\u id值?
std::array<std::pair<type_id_t, BaseType*>, 2> a = {
    std::make_pair(type_id<TypeA>, typeAInstance),
    std::make_pair(type_id<TypeB>, typeBInstance)
};
template<typename SearchType>
std::array<SearchType*, 2> GetEntriesOfType() {
    std::array<SearchType*, 2> ret;

    for(int i = 0; i < 2; i++) {
        ret[i] = nullptr;
        if(a[i].first == &type_id<SearchType>) ret[i] = a[i].second;
    }
}
template <TypeEnum> struct EnumToType;
template <> struct EnumToType<TypeA> { using type = TypeAType; };
template <> struct EnumToType<TypeB> { using type = TypeBType; };
// ...
std::array<BaseType, 2> a = { TypeAInstance, TypeBInstance };
std::array<TypeEnum, 2> b = { TypeA, TypeB };

template<SearchTypeEnum E>
std::array<typename EnumToType<E>::type*, 2> GetEntriesOfType() {
    std::array<typename EnumToType<E>::type*, 2> rc;
    for (int i = 0; i != 2; ++i) {
        rc[i] = a[i] == E? static_cast<typename EnumToType<E>::type*>(b[i]);
    }
    return rc;
}