I have a weird problem about working with integers in C++.
I wrote a simple program that sets a value to a variable and then prints it, but it is not working as expe
Making use of ADL (Argument-dependent name lookup):
#include
#include
#include
namespace numerical_chars {
inline std::ostream &operator<<(std::ostream &os, char c) {
return std::is_signed::value ? os << static_cast(c)
: os << static_cast(c);
}
inline std::ostream &operator<<(std::ostream &os, signed char c) {
return os << static_cast(c);
}
inline std::ostream &operator<<(std::ostream &os, unsigned char c) {
return os << static_cast(c);
}
}
int main() {
using namespace std;
uint8_t i = 42;
{
cout << i << endl;
}
{
using namespace numerical_chars;
cout << i << endl;
}
}
output:
*
42
A custom stream manipulator would also be possible.
cout << +i << endl
).