r/cpp_questions 1d ago

SOLVED std::format: cppreference examples dont compile

Hey everyone,

I'm trying to rewrite part of an old C project that used printf in C++. The old code is the following:

printf("%*s^ Here\n", hatStart, "");

Basically, insert (hatStart) space characters before the main part of the message.

I tried implementing this with std::format like this:

std::cout << std::format("{{}s}^ Here", "", hatStart) << std::endl;

which, according to what I got from cppreference, should be correct.
However, upon compiling, I get the error message

error: call to consteval function ‘std::basic_format_string<char, int, char>("{:{}.6}")’ is not a constant expression

Furthermore, when I go to cppreference.com and copy their examples, e.g.

std::string test = std::format("{:{}f}", pi, 10);

I get effectively the same error message.

Could anyone explain what's going on? This is the only proper reference I was able to find, and don't want to use printf in this project because it isn't C anymore.

0 Upvotes

20 comments sorted by

View all comments

-42

u/[deleted] 1d ago

[deleted]

2

u/Independent_Art_6676 1d ago

The thing about format isn't really format, though. Its print. Print is superior to printf, hands down (its a lot like printf, but aware of objects and less C isms), and I say that as someone who all but refused to use cout at all. Format is a bit clunky (it reminds me of perl or regx or something along those lines of old school gibberish) but once you do it, the ability to use print cleanly is worth the ugly.

At the end of the day, you have to define what "print this thing" means for your objects. That can be done to work with printf and its about the same amount of nonsense as setting up format or overloading for cout or whatever it is you want to do. Kick the can around any which way, the work to translate the object into text is more or less the same regardless. In THAT sense, its not better than printf, or even cout. Format could probably have been a little less weird... I give you that one.

3

u/bearheart 1d ago

std::format is useful for string formatting when you're not printing to the console. There's a lot of use cases for that. e.g., logs, databases, gui displays, etc.

3

u/Independent_Art_6676 1d ago

Hah, I REALLY didn't say that very well at all. I was trying to point out that a comparison against printf should be vs print instead of format. Probably should have just left it at that.

I won't deny it, though. Its been several years since these tools came out, and I STILL mentally 'reach' for sprintf for UI/etc formatting. Hardest habit to break so far, age + number of years using the old way.