std :: any_cast获得未命名的枚举?
在我知道任何类型之前,我都有sTD ::的排队::我在填充的众多,并且我试图施放任何未透露姓名的枚举,这些枚举被传递给了ints ints。
假设我有以下代码:
enum {
VALUE_1,
VALUE_2,
};
enum {
VALUE_3
};
template<typename ...T>
std::queue<std::any> extract_variadic_to_queue_impl(const T&... args) {
std::vector<std::any> vec = { args ... };
std::queue<std::any> ret;
for (unsigned int i = 0, max = vec.size(); i < max; i++) {
std::string type_name = vec[i].type().name();
if (type_name.starts_with("enum <unnamed-enum>")) {
ret.push((int)std::any_cast<???>(vec[i]));
}
else {
ret.push(vec[i]);
}
}
return ret;
}
template<typename ...T>
std::queue<std::any> extract_variadic_to_queue(T... args) {
return extract_variadic_to_queue_impl(std::any(args)...);
}
#define EXTRACT(var_name, type) type var_name = std::any_cast<type>(q.front()); q.pop();
void test() {
std::queue<std::any> q = extract_variadic_to_queue(
0,
"hi",
VALUE_2,
2.0f,
2.0
);
EXTRACT(my_int, int);
EXTRACT(my_string, const char*);
EXTRACT(my_constant, int);
EXTRACT(my_float, float);
EXTRACT(my_double, double);
}
我知道,对于所有未命名的枚举,type_name将评估为“枚举&lt; unumed-enum-enum&gt; - ”之后是第一个值的名称(在这种情况下,“枚举”,“枚举&lt; unnamed-enum enmed-enum&gt; -value_1”)。假设我不能只命名枚举,那么我需要用什么替换???
才能完成此工作?我知道这不是int,因为提取(my_constant,int);
抛出bad_any_cast
如果我不检查extract_variadic_to_que_queue_impl
中的未命名枚举,但是我认为必须有某种方法来获得价值,否则它根本不允许我存储它,对吗?
此外,如果test()
更改为:
void test() {
std::queue<std::any> q = extract_variadic_to_queue(
0,
"hi",
VALUE_2,
2.0f,
2.0,
VALUE_3 //now passes values from two different enums
);
EXTRACT(my_int, int);
EXTRACT(my_string, const char*);
EXTRACT(my_constant, int);
EXTRACT(my_float, float);
EXTRACT(my_double, double);
EXTRACT(my_other_constant, int);
}
谢谢!
I have queue of std::anys which I’m populating variadically before I know any of the types, and I’m trying to cast any unnamed enums which were passed to just be ints while populating it.
Suppose I have the following code:
enum {
VALUE_1,
VALUE_2,
};
enum {
VALUE_3
};
template<typename ...T>
std::queue<std::any> extract_variadic_to_queue_impl(const T&... args) {
std::vector<std::any> vec = { args ... };
std::queue<std::any> ret;
for (unsigned int i = 0, max = vec.size(); i < max; i++) {
std::string type_name = vec[i].type().name();
if (type_name.starts_with("enum <unnamed-enum>")) {
ret.push((int)std::any_cast<???>(vec[i]));
}
else {
ret.push(vec[i]);
}
}
return ret;
}
template<typename ...T>
std::queue<std::any> extract_variadic_to_queue(T... args) {
return extract_variadic_to_queue_impl(std::any(args)...);
}
#define EXTRACT(var_name, type) type var_name = std::any_cast<type>(q.front()); q.pop();
void test() {
std::queue<std::any> q = extract_variadic_to_queue(
0,
"hi",
VALUE_2,
2.0f,
2.0
);
EXTRACT(my_int, int);
EXTRACT(my_string, const char*);
EXTRACT(my_constant, int);
EXTRACT(my_float, float);
EXTRACT(my_double, double);
}
I know that for all unnamed enums, type_name will evaluate to “enum <unnamed-enum>-“ followed by the name of the first value (in this case, “enum <unnamed-enum>-VALUE_1”). Assuming I can’t just name the enum, what would I need to replace ???
with in order to make this work? I know it isn’t an int because EXTRACT(my_constant, int);
throws bad_any_cast
if I don’t check for unnamed enums in extract_variadic_to_queue_impl
, but I assume there has to be some way to get the value, otherwise it wouldn’t let me store it at all, right?
Furthermore, does anything change if test()
is changed to:
void test() {
std::queue<std::any> q = extract_variadic_to_queue(
0,
"hi",
VALUE_2,
2.0f,
2.0,
VALUE_3 //now passes values from two different enums
);
EXTRACT(my_int, int);
EXTRACT(my_string, const char*);
EXTRACT(my_constant, int);
EXTRACT(my_float, float);
EXTRACT(my_double, double);
EXTRACT(my_other_constant, int);
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用
type()。名称()
完全做出此类决定,而不是针对未命名的enum
s或任何其他类型。名称是完全定义的。无法保证他们将是什么(或者它们将是唯一的)。您应该使用
std :: Any_cast
而不是==
-croparingtype> type()
totype> type> typeID(/*)检查包含的类型。在此处键入*/)
。要检索存储的类型,您必须对该类型的一种或另一种方式具有别名。您要么给出
enum
名称,要么通过其枚举器(或其类型的变量)通过exttype
获得类型。You are not supposed to use
type().name()
to make such decisions at all, not for unnamedenum
s or any other type. The names are completely implementation-defined. There is no guarantee what they will be (or that they will be unique).You should check the contained type with
std::any_cast
instead or by==
-comparingtype()
totypeid(/*type here*/)
.To retrieve a stored type you must have an alias for that type one way or another. Either you give the
enum
a name or you obtain the type viadecltype
from its enumerator (or a variable of its type).