C++ std::any 将 std::any 的 C 字符数组转换为字符串的函数

发布于 2025-01-18 04:53:55 字数 2032 浏览 2 评论 0原文

#include <iostream>
#include <any>
#include <string>
#include <vector>
#include <map>
using namespace std;

string AnyPrint(const std::any &value)
{   
    cout << size_t(&value) << ", " << value.type().name() << " ";
    if (auto x = std::any_cast<int>(&value)) {
        return "int(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<float>(&value)) {
        return "float(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<double>(&value)) {
        return "double(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<string>(&value)) {
        return "string(\"" + (*x) + "\")";
    }
    if (auto x = std::any_cast<char*>(&value)) {
        return string(*x);
    }
}

int main()
{
    int a = 1;
    float b = 2;
    double c = 3;
    string d = "4";
    char *e = "555";
    
    cout << AnyPrint(a) << "\n";
    cout << AnyPrint(b) << "\n";
    cout << AnyPrint(c) << "\n";
    cout << AnyPrint(d) << "\n";
    cout << AnyPrint("555") << "\n";
    cout << AnyPrint(e) << "\n";
    return 0;
}

我正在尝试创建一个将 std::any 对象转换为字符串的函数,因为可能的类型列表是硬编码的。但是,当用户解析像 AnyPrint("555") 这样的原始字符串时会出现问题。我使用 在没有 RTTI 的情况下检查 std::any 的类型 中的方法

我得到以下输出当我运行程序时:

140722480985696, i int(1)
140722480985696, f float(2.000000)
140722480985696, d double(3.000000)
140722480985696, NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE string("4")
140722480985696, PKc string("4")
140722480985696, Pc 555

如何处理原始字符串的 std::any ?我不想写 AnyPrint("555"s) 除非这是唯一的方法。

编辑:我用它来运行示例 https://www.onlinegdb.com/online_c++_compiler

#include <iostream>
#include <any>
#include <string>
#include <vector>
#include <map>
using namespace std;

string AnyPrint(const std::any &value)
{   
    cout << size_t(&value) << ", " << value.type().name() << " ";
    if (auto x = std::any_cast<int>(&value)) {
        return "int(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<float>(&value)) {
        return "float(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<double>(&value)) {
        return "double(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<string>(&value)) {
        return "string(\"" + (*x) + "\")";
    }
    if (auto x = std::any_cast<char*>(&value)) {
        return string(*x);
    }
}

int main()
{
    int a = 1;
    float b = 2;
    double c = 3;
    string d = "4";
    char *e = "555";
    
    cout << AnyPrint(a) << "\n";
    cout << AnyPrint(b) << "\n";
    cout << AnyPrint(c) << "\n";
    cout << AnyPrint(d) << "\n";
    cout << AnyPrint("555") << "\n";
    cout << AnyPrint(e) << "\n";
    return 0;
}

I'm trying to make a function that converts a std::any object to string, given that the list of possible types is hard-coded. However, there's a problem when user parse raw string like AnyPrint("555"). I use the method from Checking std::any's type without RTTI

I got the following output when I run the program:

140722480985696, i int(1)
140722480985696, f float(2.000000)
140722480985696, d double(3.000000)
140722480985696, NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE string("4")
140722480985696, PKc string("4")
140722480985696, Pc 555

How can I handle a std::any of raw string? I don't want to write AnyPrint("555"s) unless it's the only way.

Edit: I use this to run the example https://www.onlinegdb.com/online_c++_compiler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

疾风者 2025-01-25 04:53:55

的类型“ 555”const char [4],可能会衰减到const char*。您可以处理char*,但不是const char*

处理const char*修复了您的问题:

std::string AnyPrint(const std::any &value)
{   
    std::cout << size_t(&value) << ", " << value.type().name() << " ";
    if (auto x = std::any_cast<int>(&value)) {
        return "int(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<float>(&value)) {
        return "float(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<double>(&value)) {
        return "double(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<std::string>(&value)) {
        return "string(\"" + (*x) + "\")";
    }
    if (auto x = std::any_cast<const char*>(&value)) {
        return *x;
    }
    return "other";
}

demo

Type of "555" is const char[4] which might decays to const char*. You handle char*, but not const char*.

Handling const char* fixes your issue:

std::string AnyPrint(const std::any &value)
{   
    std::cout << size_t(&value) << ", " << value.type().name() << " ";
    if (auto x = std::any_cast<int>(&value)) {
        return "int(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<float>(&value)) {
        return "float(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<double>(&value)) {
        return "double(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<std::string>(&value)) {
        return "string(\"" + (*x) + "\")";
    }
    if (auto x = std::any_cast<const char*>(&value)) {
        return *x;
    }
    return "other";
}

Demo

故事灯 2025-01-25 04:53:55

正如评论中提到的,像 "555" 这样的字符串文字是(或衰减为)const char* 指针。您的 AnyPrint 函数不处理此类参数类型。

添加以下块可以解决该问题:

    if (auto x = std::any_cast<const char*>(&value)) {
        return string(*x);
    }

另外,请注意,char *e = "555"; 行在 C++ 中是非法的;您需要 const char *e = "555";char e[] = "555";;使用后者将演示 char*(使用 AnyPrint(e))和 const char*(使用 AnyPrint( "555")) 在 std::any_cast 块中键入。

As mentioned in the comments, string literals like "555" are (or decay to) const char* pointers. Your AnyPrint function doesn't handle such an argument type.

Adding the following block fixes the problem:

    if (auto x = std::any_cast<const char*>(&value)) {
        return string(*x);
    }

Also, note that the line, char *e = "555"; is illegal in C++; you need either const char *e = "555"; or char e[] = "555";; using the latter will demonstrate the difference between the char* (with AnyPrint(e)) and const char* (with AnyPrint("555")) types in the std::any_cast<T> blocks.

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