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
  • 17,272
  • 18
  • 86
  • 144
  • Short answer: No. – churill 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 from Microsoft. 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.2.

Jasper Li
  • 19
  • 3