在C++中根据运行时字符串选择模板

Choose template based on run-time string in C++

本文关键字:字符串 选择 运行时 C++      更新时间:2023-10-16

我有一个可以容纳不同类型的属性向量:

class base_attribute_vector; // no template args
template<typename T>
class raw_attribute_vector : public base_attribute_vector;
raw_attribute_vector<int> foo;
raw_attribute_vector<std::string> foo;

基于该类型的运行时输入,我希望创建适当的数据结构。伪码:

std::string type("int");
raw_attribute_vector<type> foo;

显然,这是失败的。一个简单但丑陋且无法维护的解决方法是运行时切换/连锁if:

base_attribute_vector *foo;
if(type == "int") foo = new raw_attribute_vector<int>;
else if(type == "string") ...

我读过关于函数的运行时多态性的文章,但发现对于一个概念上很容易的任务来说,它相当复杂。

什么是最好、最干净的方法来实现这一点?我试用了boost::hana,发现虽然我可以创建从字符串到类型的映射,但查找只能在编译时完成:

auto types = 
hana::make_map(
    hana::make_pair(BOOST_HANA_STRING("int"), hana::type_c<int>),
    hana::make_pair(BOOST_HANA_STRING("string"), hana::type_c<std::string>)
);

所有可能的类型在编译时都是已知的。如有任何建议,我们将不胜感激。在一个完美的解决方案中,我会在一个地方创建名称->类型映射。之后,我会像这个一样使用它

std::vector<base_attribute_vector*> foo;
foo.push_back(magic::make_templated<raw_attribute_vector, "int">);
foo.push_back(magic::make_templated<raw_attribute_vector, "std::string">);
foo[0]->insert(123);
foo[1]->insert("bla");
foo[0]->print();
foo[1]->print();

这种魔术不需要在编译时发生。我的目标是拥有尽可能可读的代码。

我会使用一个以字符串为键、以std::function为值的std::map。我会将字符串与一个返回您的类型的函数相关联。这里有一个例子:

using functionType = std::function<std::unique_ptr<base_attribute_vector>()>;
std::map<std::string, functionType> theMap;
theMap.emplace("int", []{ return new raw_attribute_vector<int>; });
theMap.emplace("float", []{ return new raw_attribute_vector<float>; });
// Using the map
auto base_vec = theMap["int"](); // base_vec is an instance of raw_attribute_vector<int>

当然,如果您只在运行时知道字符串值,则此解决方案是有效的。

enum class Type
{
    Int,
    String,
    // ...
    Unknown
};
Type TypeFromString(const std::string& s)
{
    if (s == "int") { return Type::Int; }
    if (s == "string") { return Type::String; }
    // ...
    return Type::Unknown;
}
template <template <typename> class>
struct base_of;
template <template <typename> class C>
using base_of_t = typename base_of<C>::type;

然后是通用工厂

template <template <typename> class C>
std::unique_ptr<base_of_t<C>> make_templated(const std::string& typeStr)
{
    Type type = TypeFromString(typeStr);
    static const std::map<Type, std::function<std::unique_ptr<base_of_t<C>>()>> factory{
        {Type::Int, [] { return std::make_unique<C<int>>(); } },
        {Type::String, [] { return std::make_unique<C<std::string>>(); } },
        // ...
        {Type::Unknown, [] { return nullptr; } }
    };
    return factory.at(type)();
}

每个基地都需要一个专业化:

template <>
struct base_of<raw_attribute_vector> {
    using type = base_attribute_vector;
};

然后

auto p = make_templated<raw_attribute_vector>(s);

演示

我可能会这样做:

特点:

  • 通过命名的原型对对象进行1次注册

  • 运行时的恒定时间查找

  • 可与std::string 进行比较的任何类型的查找

-

#include <unordered_map>
#include <string>

struct base_attribute_vector { virtual ~base_attribute_vector() = default; };
template<class Type> struct attribute_vector : base_attribute_vector {};
// copyable singleton makes handling a breeze    
struct vector_factory
{
    using ptr_type = std::unique_ptr<base_attribute_vector>;
    template<class T>
    vector_factory add(std::string name, T)
    {
        get_impl()._generators.emplace(std::move(name),
                                       []() -> ptr_type
                                       {
                                           return std::make_unique< attribute_vector<T> >();
                                       });
        return *this;
    }
    template<class StringLike>
    ptr_type create(StringLike&& s) const {
        return get_impl()._generators.at(s)();
    }
private:
    using generator_type = std::function<ptr_type()>;
    struct impl
    {
        std::unordered_map<std::string, generator_type, std::hash<std::string>, std::equal_to<>> _generators;
    };

private:
    static impl& get_impl() {
        static impl _ {};
        return _;
    }
};

// one-time registration
static const auto factory =
vector_factory()
.add("int", int())
.add("double", double())
.add("string", std::string());

int main()
{
    auto v = factory.create("int");
    auto is = vector_factory().create("int");
    auto strs = vector_factory().create("string");

}

主要基于Jarod42的答案,这就是我将使用的:

class base_attribute_vector {};
template<typename T>
class raw_attribute_vector : public base_attribute_vector {
public:
raw_attribute_vector() {std::cout << typeid(T).name() << std::endl; }
};
template<class base, template <typename> class impl>
base* magic(std::string type) {
    if(type == "int") return new impl<int>();
    else if(type == "float") return new impl<float>();
}
int main() {
    auto x = magic<base_attribute_vector, raw_attribute_vector>("int");
    auto y = magic<base_attribute_vector, raw_attribute_vector>("float");
}

简短回答:不,您不能指示编译器在编译时评估运行时条件。甚至连哈娜都没有。

长话短说:有一些(主要是与语言无关的)模式。

我假设您的base_attribute_vector有一些virtual方法,很可能是pure,在其他语言中通常称为interface

这意味着,根据实际问题的复杂性,你可能想要一个工厂或抽象工厂。

您可以在C++中创建一个没有虚拟方法的工厂或抽象工厂,并且可以使用hana。但问题是:增加的复杂性真的值得(可能真的很小)提高性能吗?

(此外,如果您想消除每个虚拟调用,即使是从base_attribute_vector调用,也必须在发生切换的入口点之后,使用该类使所有东西成为模板)

我的意思是,您是否用虚拟方法实现了这一点,并衡量了虚拟调用的成本太大?

编辑:另一个但不同的解决方案可能是对访客使用变体类型,比如鸡蛋:变体。

使用variant,可以为每个参数类型创建具有函数的类,apply方法将根据其运行时类型切换要运行的函数。

类似于:

struct handler {
  void operator()(TypeA const&)  { ... }
  void operator()(TypeB const&)  { ... }
  // ...
};
eggs::variant< ... > v;
eggs::variants::apply(handler{}, v);

您甚至可以使用模板化运算符(可能与enable_if/sfinae一起使用),如果它们有公共部分的话。