As we all know, if for some reason you are printf()something, not a stream (which is rare, but can sometimes happen), you need to specify the appropriate format specifier ( dfor signed ints, ufor unsigned, etc., etc.) d.). Now, since it printf()is part of the C ++ standard library, and not just some C legacy, I hope it could be something like
printf()
d
u
template <typename T> const std::string format_specifier;
That will allow, say:
template <typename Foo> void bar(const Foo& my_foo) { printf(format_specifier<Foo>, my_foo); }
Is there something similar in the standard library?
Notes:
std::cout
, .
- "", ++ .
, C ( , , std ..), , ++ iostream , - .
iostream
++ .
-, , ++ , ++ stream. - , , ostream& operator << (ostream&, const T&).
stream
ostream& operator << (ostream&, const T&)
, printf (Python, Java), , , , io-. , :
printf("Operation %-*s : %10.2f$ %2d%/02d\n", size, lib, val, dat->mday, dat->mon + 1);
( (#))
#
- C ++ - , .
, , :
template<typename T> char const *format_specifier(); template<> inline char const *format_specifier<int>() { return "%d"; } // ... in function int d = 5; printf(format_specifier<decltype(d)>, d);
IMHO, . , .
The best solution using printf formatting would be code:
my_printf("bla % bla % bla", arg1, arg2, arg3);
where %just gets the correct type from the argument. You can do this using variable templates, and there are already some popular implementations.
%