C++ std::any 将 std::any 的 C 字符数组转换为字符串的函数
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
的类型“ 555”
是const char [4]
,可能会衰减到const char*
。您可以处理char*
,但不是const char*
。处理
const char*
修复了您的问题:demo
Type of
"555"
isconst char[4]
which might decays toconst char*
. You handlechar*
, but notconst char*
.Handling
const char*
fixes your issue:Demo
正如评论中提到的,像
"555"
这样的字符串文字是(或衰减为)const char*
指针。您的AnyPrint
函数不处理此类参数类型。添加以下块可以解决该问题:
另外,请注意,
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. YourAnyPrint
function doesn't handle such an argument type.Adding the following block fixes the problem:
Also, note that the line,
char *e = "555";
is illegal in C++; you need eitherconst char *e = "555";
orchar e[] = "555";
; using the latter will demonstrate the difference between thechar*
(withAnyPrint(e)
) andconst char*
(withAnyPrint("555")
) types in thestd::any_cast<T>
blocks.