A way to get python like f-strings in C++?
Humor me for a second :-) I know we don't aim to be like python, but I think we should always look to other languages for inspiration.
So python has something called f-strings, which are basically a really nice way of doing string formatting. It generally looks like this:
name = "Evan"
greeting = f"Hello, {name}"
As you can see it embeds the variables that it wants to substitute from into the string itself which is very convenient. Not only that, but it is shorter, and arguably less error prone since argument misplacement will stand out more...
And we almost have this in C++20 with the addition of the fmt lib!
auto name = "Evan"
auto greeting = std::format("Hello {}", name);
So what if a future version of the standard had a compiler supported transformation. Where if you wrote something like this:
f"Hello {name}"
it would be implicitly converted to this:
std::format("Hello {}", name)
The only obvious to me downside is that we'd have language feature intimately intertwined with a library feature, but there's precedent for that such as std::initializer_list.
Even things like range based for-loops and lambdas are basically the compiler saying "you wrote X, I'm going to expand it to Y for you".
I think the compiler can easily detect if you used an f-string, but forgot to include the <format> header, and simply error saying that you need to include it. In fact, I'm pretty sure I've seen such errors already.
So what does the community think? Good idea? Bad Idea?
82 comments · 85% upvoted · other discussions
I guess it would be convenient but idk if it's necessary to have as a language feature.
What I would like to see is reflection support that's good enough to allow doing something like this as a library feature.
eteran [OP] 7 points · Apr 2023
Well, I don't think we can do much better than
std::formatwithout some form of ability to do a transformation at a syntactic level. Maybe if we had Sutter's meta-classes or similar, but I don't think reflection alone is good enough, we'd need not only reflection, but also transformation.aocregacc 5 points · Apr 2023
Yeah you need some sort of injection capability, just querying things at compile time isn't enough. Maybe that doesn't fall under reflection, I thought it did.
I think if you had the ability to turn compile time strings into identifiers you could use a user defined literal and do a simple version like "hello {name}"f.If you want arbitrary expressions inside {} you would probably need more.
Edit: nevermind a UDL doesn't work here, so some more transformation capabilities are needed.
eteran [OP] 4 points · Apr 2023
It might, I'm not an expert or any authority on what "reflection" means, I just have always considered it to be "code that can inspect itself" :shrug:
very_curious_agent 2 points · Apr 2023
Get rid of old #macros, bring in lisp.
eteran [OP] 8 points · Apr 2023
BTW, I agree that such a feature is not "necessary".
But I'm also a firm advocate of "if two pieces of code do the same work, but one is shorter without sacrificing clarity, then the shorter one is better".