Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++ standard library have a templated getter for the printf conversion specifier?

As we all know, if for some reason you printf() something rather than stream it (which rarely, but sometimes, might happen), you need to specify an appropriate format specifier (d for signed int, u for unsigned, etc. etc.). Now, since printf() is part of the C++ standard library and not just some C legacy, I would hope there might be something like

template <typename T> 
const std::string format_specifier;

Which would allow, say:

template <typename Foo> 
void bar(const Foo& my_foo) {
    printf(format_specifier<Foo>, my_foo);
}

Does the standard library contain something like that?

Notes:

  • Please don't suggest using std::cout, of course that's the default thing you should do. I asked about a function that's part of the standard library; if it were removed from it you could say "this should never ever be used, so your question doesn't matter".
like image 363
einpoklum Avatar asked Feb 08 '23 09:02

einpoklum


1 Answers

I originally wrote this as a comment, since it seemed too short.

The short answer is "no", since the C++ standard does not specify any such facilities.

Given properties of functions that are inherited from the C standard library for reasons of backward compatibility (no overloading, permitted in both the global namespace and namespace std, etc), and the way that using C++ iostream classes is encouraged, I would be surprised if such a feature ever comes to be standardised.

like image 103
Peter Avatar answered Feb 13 '23 11:02

Peter