如何将枚举传递给模板参数

时间:2016-12-20 12:47:28

标签: c++ c++11 templates enums

我有一个xml,我从需要创建的对象的类型中读取,问题在于如何在不使用switch / if语句的情况下传递枚举。

   enum ObjectType {A,B,C};

   void parseXML(const string& fileName)
   {
     //Open-read file etc...

     ObjectType objType = xmlObject.type(); <- the structure provided from the xml parser that I use(codesynthesis)

     ObjectParameters params = gatherParameters(xmlObject);

     auto createdObj = factory.createObject<objType>(params);
                                           ^^^^^
  }

需要一个常量表达式,所以我必须映射提供的类型还是有更快的方法? 如果是这样,有一种方法可以将枚举用作类的标记/同义词吗?

1 个答案:

答案 0 :(得分:3)

auto magic_switch=[]( auto value, auto limit ){ // limit must be compile time type value
  return [value,limit](auto&&f){
    auto* pf=std::addressof(f);
    using ptr=decltype(pf);
    auto index=indexer<limit>();
    using R=decltype((decltype(f)(*pf))(limit));
    using Sig=R(*)(ptr pf);
    static const auto table=index(
      [](auto...Is)
      ->std::array<Sig, decltype(limit){}>
      {
        return {{
          +[](ptr pf)->R
          {
            return (decltype(f)(*pf))( decltype(Is){} );
          }...
        }};
      }
    );
    return table[value](pf);
  };
};

索引器在哪里

template<std::size_t I>using index_t=std::integral_constant<std::size_t, I>;
template<std::size_t I>constexpr index_t<I> index_k{};
template<class=void, std::size_t...Is>
auto indexer(std::index_sequence<Is...>){
  return [](auto&&f){
    return f( index_k<Is>... );
  };
}

template<std::size_t N>
auto indexer(){
  return indexer(std::make_index_sequence<N>{});
}

然后你的代码;

auto createdObj = factory.createObject<objType>(params);
// work with it

变为:

magic_switch( objType, index_k<3> )
([&](auto index){
  auto createdObj = factory.createObject<(ObjectType)index>(params);
  // work with it
});

live example

注意你最终在lambda中的子视图中;没有办法避免这种情况。

相关问题