Add ToString function for array and tuple#4584
Conversation
|
I am not sure how to read this |
|
I think that one is confusing because the second type of the map is vector, and the two vectors in the map have different sizes. Here is a more typical example: std::cout << amrex::ToString(std::map<std::string, double> {
{"pi", 3.14159265358979323846},
{"clight", 299'792'458.},
{"ep0", 8.8541878188e-12},
{"mu0", 1.2566370612685e-06}
}) << '\n'; |
|
Should we treat |
|
On the other hand, if we do not treat char* as string, it's not any better either. There could still be undefined behavior if we pass a produces |
|
It could indeed be quite tricky to see whether something is a string or a data buffer. Given the limited information, I would suggest this: |
Summary
This PR adds a
ToStringfunction that can be used with scalars, arrays and tuples to write error messages or when debugging.TODO: write a doc comment for
ToStringIt might be useful to add compatibility for BaseFab and PODVector with automatic dtoh memcpy.
Example (https://godbolt.org/z/vsn6Krbsj)
std::vector v{4., 7., 23., 0.000001, 7.123456789123456789, 8., 3., 0.0000011}; std::tuple t{443, 0.234423, 244.4f, "Test tuple"}; std::array a{4., 7., 23., 0.000001, 7.12345, 8., 3., 0.0000011}; std::map<int, std::vector<double>> m{{0, {32.42, 1.000324}}, {4, {12.3, 2.65775, 3.24}}}; int b[]{2, 3, 65, 3, 6, 54}; const char* c = "Test char *"; char e[] = "Test char array"; std::cout << amrex::ToString(v) << '\n'; std::cout << amrex::ToString(t, "[", ",", "]", "'", 100,std::ostringstream{} << std::setprecision(4)) << '\n'; std::cout << amrex::ToString(a, "{", " ; ", "}", "\"", 3, std::ostringstream{} << std::setprecision(10)) << '\n'; std::cout << amrex::ToString(m, "<", " | ", ">", "\"", 100, std::ostringstream{} << std::setprecision(10))<< '\n'; std::cout << amrex::ToString(b) << '\n'; std::cout << amrex::ToString(c) << '\n'; std::cout << amrex::ToString(e) << '\n'; std::cout << amrex::ToString("Test direct string") << '\n'; std::cout << amrex::ToString(std::string("Test string")) << '\n'; std::cout << amrex::ToString(std::array{"awdawd", "43tf4", "awd4t45"}) << '\n'; std::cout << amrex::ToString(std::array{'t', 'e', 's', 't'}) << '\n'; std::cout << '\n'; amrex::ToString(std::cout, v) << '\n'; amrex::ToString(std::cout << std::setprecision(4), t, "[", ",", "]", "'", 100) << '\n'; amrex::ToString(std::cout << std::setprecision(10), a, "{", " ; ", "}", "\"", 3) << '\n'; amrex::ToString(std::cout << std::setprecision(10), m, "<", " | ", ">", "\"", 100) << '\n'; amrex::ToString(std::cout, b) << '\n'; amrex::ToString(std::cout, c) << '\n'; amrex::ToString(std::cout, e) << '\n'; amrex::ToString(std::cout, "Test direct string") << '\n'; amrex::ToString(std::cout, std::string("Test string")) << '\n'; amrex::ToString(std::cout, std::array{"awdawd", "43tf4", "awd4t45"}) << '\n'; amrex::ToString(std::cout, std::array{'t', 'e', 's', 't'}) << '\n';Prints
Additional background
Checklist
The proposed changes: