[英]How to cast each variant type with variadic template
What I want to do is to cast each "VariantType" (union of different types) argument to it's type before calling some function from a shared library.我想要做的是在从共享库调用一些 function 之前将每个“VariantType”(不同类型的联合)参数转换为它的类型。 what I'm doing so far now is down below.
到目前为止,我正在做的事情在下面。 it's just 3 different types and 2 arguments and it takes a lot lines.
它只有 3 种不同的类型和 2 个 arguments 并且需要很多行。 but I want to achieve this with variant number of argument with 7 different types.
但我想用 7 种不同类型的变体参数来实现这一点。 this has to do something with variadic template( another relevant question: Template tuple - calling a function on each element ).
这与可变参数模板有关(另一个相关问题: 模板元组 - 在每个元素上调用 function )。 or if you know some better way let me know.
或者,如果您知道更好的方法,请告诉我。
template<typename... T>
int call(const char* func_name, T... args) {
// this will call func_name(args...) somehow from a dll binary.
// If you need the implementation : https://hastebin.com/ocoyaniruj.cpp
}
int main(int argc, char** argv) {
const char* func_name = "func_name";
VariantType arg1 = "hello world!";
VariantType arg2 = 3.14;
if (arg1.get_type() == VariantType::INT) {
if (arg2.get_type() == VariantType::INT) {
call(func_name, (int)arg1, (int)arg2);
} else if (arg2.get_type() == VariantType::FLOAT){
call(func_name, (int)arg1, (float)arg2);
} else if (arg1.get_type() == VariantType::STRING){
call(func_name, (int)arg1, arg2.c_str());
}
} else if (arg1.get_type() == VariantType::FLOAT){
if (arg2.get_type() == VariantType::INT) {
call(func_name, (float)arg1, (int)arg2);
} else if (arg2.get_type() == VariantType::FLOAT){
call(func_name, (float)arg1, (float)arg2);
} else if (arg1.get_type() == VariantType::STRING){
call(func_name, (float)arg1, arg2.c_str());
}
} else if (arg1.get_type() == VariantType::STRING){
if (arg2.get_type() == VariantType::INT) {
call(func_name, arg1.c_str(), (int)arg2);
} else if (arg2.get_type() == VariantType::FLOAT){
call(func_name, arg1.c_str(), (float)arg2);
} else if (arg1.get_type() == VariantType::STRING){
call(func_name, arg1.c_str(), arg2.c_str());
}
}
return 0;
}
You have a messy, and very partial, runtime reflection mechanism.你有一个混乱的、非常部分的运行时反射机制。 I'm sure you're feeling a lot of pain with this stuff... so the first things to consider would be:
我敢肯定你对这些东西感到很痛苦......所以首先要考虑的是:
Having said that - you could, in many cases, be satisfied with C++'s own variant type: std::variant
introduced in C++17, together with std::visit
, and a templated visitor (as in this question - but with two templates).话虽如此 - 在许多情况下,您可以对 C++ 自己的变体类型感到满意:在 C++17 中引入的
std::variant
以及std::visit
和一个模板化的访问者(如在这个问题中 - 但有两个模板)。
Here's what this would look like:这是它的样子:
#include <variant>
#include <iostream>
#include <string>
int main() {
using variant_type = std::variant<int, float, std::string>;
variant_type v1{"hello world!"};
variant_type v2{3.14f};
std::visit([](auto&& x, auto&& y) {
// I used this as a stub:
//
// std::cout << x << " , " << y << '\n';
//
// but you want:
call(func_name, x, y);
}, v1, v2);
}
There is one caveat, though - this won't extract the c_str()
from your strings.但是,有一个警告 - 这不会从您的字符串中提取
c_str()
。 If you want to also do that, you can do one of two things:如果你也想这样做,你可以做以下两件事之一:
const char*
in the variant to begin with.const char*
开始。x
and y
, you can have a templated transformer function, which does nothing usually, but applies .c_str()
for const string references.x
和y
之外,您还可以使用模板化转换器 function,它通常什么都不做,但将.c_str()
应用于 const 字符串引用。 but I don't really like the second approach.但我不太喜欢第二种方法。
I've finally found the simple way to cast each argument with a variadic template (kinda reinvent the wheel std::visit
).我终于找到了使用可变参数模板强制转换每个参数的简单方法(有点重新发明轮子
std::visit
)。
template<typename... Targs>
void visit(const char* func_name, int visiting_arg, VariantType front, Targs... p_args) {
if (visiting_arg == 0) {
call(func_name, p_args...);
return;
}
if (front.get_type() == VariantType::INT) {
visit(func_name, visiting_arg-1, p_args..., (int)front);
} else if (front.get_type() == VariantType::FLOAT) {
visit(func_name, visiting_arg - 1, p_args..., (float)front);
} else if (front.get_type() == VariantType::STRING) {
visit(func_name, visiting_arg - 1, p_args..., front.c_str());
}
}
int main(int argc, char** argv) {
const char* func_name = "func_name";
int argcount = 3;
VariantType s = "Hello world!";
VariantType f = 3.14;
VariantType i = 42;
visit(func_name, argcount, s, f, i, VariantType("--placeholder--"));
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.