2

I found this answer, which is nice, but not enough for me.

They offer

boost::format("error! value was %1% but I expected %2%") % actualValue % expectedValue

Which is nice, but much less readable than what I would like (in some syntax):

boost::format("error! value was %actualValue % but I expected %expectedValue%")

In C# that is

$"error! value was {actualValue} but I expected {expectedValue}"

In Python that is

f"error! value was {actualValue} but I expected {expectedValue}"

Is that supported somehow in C++?

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • Short answer: No. – Lukas-T Jul 28 '20 at 09:00
  • 1
    Note that the C++20 standard adds [`std::format`](https://en.cppreference.com/w/cpp/utility/format/format). It still doesn't "fix" your wanted syntax though, which depends on the compiler not really doing any kind of parsing of string literals (besides handling escape sequences). – Some programmer dude Jul 28 '20 at 09:03
  • 2
    You could use the stream syntax if you find it closer to what you want to achieve. – Fareanor Jul 28 '20 at 09:06
  • You could consider offering a bounty on the linked question instead of reposting. – cigien Jul 28 '20 at 13:27
  • Not sure how close this is to what you want, but what you could do is to store your values in a map and then have your own replacement routines. Like `map variables; variables["actualValue"] = 1; cout << replace_variables("The actual value is $actualValue$", variables);`, with the function containing a loop over the map and appropriate replacement calls. – Aziuth Feb 21 '22 at 09:36

1 Answers1

0

There is a C++ standard proposal. Interpolated literals

The samples.

int port = 27015;
std::cout << f"Connecting on port {port}...\n";
int get_result() { return 42; }
std::cout << f"The result is {get_result()}\n";

I like this kind of syntax. It's concise and beautiful.

Unfortunately, there is no real C++ implementation libraries until 2022.8.

Jasper Li
  • 81
  • 5
  • 1
    I wrote the proposal, and I am not associated with Microsoft. There is also no implementation of this proposal whatsoever yet. – Vittorio Romeo Aug 04 '22 at 15:02
  • I'm Sorry, Vittorio, I misunderstood something with your email address in the proposal. The answer has been revised. By the way, I'm learning C++ now, and I know C++ has many advanced weapons. Can we implement it by current C++'s weapons? or does it need some compiler's support to achieve it? I use f-string in python, it supplys efficiency. I'm looking forward to this efficiency in C++, too. Thanks for your proposal, Vittorio. :) – Jasper Li Aug 05 '22 at 06:43