在C++中根据运行时字符串选择模板
Choose template based on run-time string in C++
我有一个可以容纳不同类型的属性向量:
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一起使用),如果它们有公共部分的话。
- Switch 语句(字符串)一直选择默认值,除非其为零
- 使用 switch,但我想用字符串而不是 int 进行选择
- 如何在C++中使用正则表达式"|"后选择字符串?
- 如何从字符串向量中选择第 n 个位置?
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- 选择随机字符串数组的排序
- 当通过常量和不是字符串的最佳选择时,是否有任何情况?
- 使用C 中的一系列字符串的选择排序
- "我该如何展示cout<<"发烧";因为我不熟悉字符串数组,它也卡在选择部分?
- 如何根据定义的字符串类型在“std::cout”和“std::wcout”之间进行选择
- 我如何在众多字符串中选择一个并显示所有可能的结果
- 以一种方式使用字符串选择(如果语句)
- C 选择排序字符串数组
- 如何在字符串中选择句子的一部分 (C++)
- c ++ mysql 连接器从字符串中选择 id
- 在菜单前键入字符串后,C 如何从菜单中选择选项
- 基于字符串选择成员函数
- 使用字符串选择模板类的专用化
- C++中的字符串选择排序
- 在C++中根据运行时字符串选择模板