Doing a cast, and a C cast at that, is just so fooked......but this is MS, after all.
Using C++ casts is gonna be more than its usual bit cumbersomeness nature:
12 13 14
|
std::cout << std::format("&u = {:p} &s = {:p}\n",
reinterpret_cast<void*>(&u),
reinterpret_cast<void*>(const_cast<int*>(&s)));
|
As
yonked as that code looks, it does show how
borked-up things are with this MS implementation of
std::format to display addresses.
That's
muh opinion, and I'm stickin' to it! Casting, PFFFFFFFFFFFFFFFT!
Just for '
freshers, the entire bit of modularized C++20 that does work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
import <iostream>;
import <format>;
int main()
{
unsigned int u { 42 };
const int& s { static_cast<int>(u) };
std::cout << std::format("u = {} s = {}\n", u, s);
std::cout << std::format("&u = {:p} &s = {:p}\n",
reinterpret_cast<void*>(&u),
reinterpret_cast<void*>(const_cast<int*>(&s)));
u = 6 * 9;
std::cout << std::format("u = {} s = {}\n", u, s);
}
|
Yes, I know I could do
#includes instead of
imports, but for me it's a style thing. I can see at a glance this is C++20 (or later) code.
More and more I am really liking
std::format. Displaying nicely formatted tables is ridiculously easy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
import <iostream>;
import <format>;
int main()
{
unsigned int limit { };
std::cout << "This program calculates n! and the sum of the integers "
<< "up to n for values 1 to limit.\n";
std::cout << "What upper limit for n would you like? ";
std::cin >> limit;
// the format string for all rows of the table
const auto table_format { "{:>8} {:>8} {:>20}\n" };
// output column headings
std::cout << std::format(table_format, "integer", "sum", "factorial");
for (unsigned long long n { 1 }, sum {}, factorial { 1 }; n <= limit; ++n)
{
sum += n;
factorial *= n;
std::cout << std::format(table_format, n, sum, factorial);
}
}
|
Speaking of import vs. #include, several resources I've consulted/read mention importing the C++ library's C headers (
<cstdio> for example) shouldn't work/be allowed. Well, with VS that just ain't true.
Importing
<windows.h> appears to work as well.....