std :: any_cast获得未命名的枚举?

发布于 2025-02-09 06:46:25 字数 2133 浏览 1 评论 0原文

在我知道任何类型之前,我都有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2025-02-16 06:46:27

您不应该使用type()。名称()完全做出此类决定,而不是针对未命名的enum s或任何其他类型。名称是完全定义的。无法保证他们将是什么(或者它们将是唯一的)。

您应该使用std :: Any_cast而不是== -croparing type> type() to type> type> typeID(/*)检查包含的类型。在此处键入*/)

要检索存储的类型,您必须对该类型的一种或另一种方式具有别名。您要么给出enum名称,要么通过其枚举器(或其类型的变量)通过exttype获得类型。

You are not supposed to use type().name() to make such decisions at all, not for unnamed enums 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 ==-comparing type() to typeid(/*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 via decltype from its enumerator (or a variable of its type).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文