1

I want to create an F string for C++, as in Python. But I don’t understand how the expression in curly braces {} is parsed.

Example (Python):

world = "world"
print(f "Hello {world}")

How do I do the same, but in C++?

std::string world = "world!"
std::cout << f("Hello {world}") << std::endl;

Output

Hello world!
Yaniboze
  • 73
  • 6
  • 3
    I would say that is impossible in C++, because it requires compiled C++ code to have knowledge of the names of C++ variables. That doesn't happen in C++. – john Jun 18 '20 at 06:31
  • Exactly like this example, is currently not possible in C++ – Robert Andrzejuk Jun 18 '20 at 06:33
  • You won't be able to do that. You can't look up a variable by it's name using a string in c++. It's a completely statically typed language with little to no reflection. – super Jun 18 '20 at 06:33
  • @super I wouldn't say "You won't be able". Reflection is coming to C++. – Robert Andrzejuk Jun 18 '20 at 06:37
  • @RobertAndrzejuk I would say that splitting hairs is fairly pointless. – super Jun 18 '20 at 06:39
  • 1
    From https://www.boost.org/doc/libs/1_54_0/libs/format/doc/format.html, the example is `cout << boost::format("writing %1%, x=%2% : %3%-th try") % "toto" % 40.23 % 50;` which is probably as close as you can get to what you want. – stefaanv Jun 18 '20 at 06:41
  • Or maybe there exists precompiler out there (or you write your own) that replaces such commands with the appropriate C++ code, such that your example would be precompiled to `std::cout << "Hello " << world << std::endl;` – Lukas-T Jun 18 '20 at 06:44
  • @super I doing understand what "splitting hairs" means? Last year the C++ comitee accepted the technical specifiction for Reflection. (In theory this could be implemented NOW by any compiler) This is one of the major goals for C++23. Maybe this comment will be read a few years from today, then it will be misleading. – Robert Andrzejuk Jun 18 '20 at 06:48
  • @RobertAndrzejuk Getting pedantic. We can only speculate on what C++ may or may not do in the future. Today it does not offer reflection. This cannot be implemented as-is. A C++ function cannot discover what variables were set in the caller scope and behave differently. – tadman Jun 18 '20 at 06:58
  • It looks like [F Strings](https://www.python.org/dev/peps/pep-0498/) are a language syntax feature, so until C++ has such a thing it's not possible. – tadman Jun 18 '20 at 07:00
  • @RobertAndrzejuk It's not going to be possible with current reflexion TS either it seems. – n. m. could be an AI Jun 18 '20 at 07:11
  • @RobertAndrzejuk Google says *Splitting hairs - make small and overfine distinctions*. **If** someone happens to read this several years from now **and** also misinterprets what I said **and** does not check on the date of posting **and** so on... it **might** incovenience someone a little bit. Hardly seems discussion worthy. – super Jun 18 '20 at 07:12
  • 1
    You can do this with a slightly different syntax: `std::cout << "Hello " << world << std::endl;` It's a bit too verbose and annoying but it works. Hope this helps. – n. m. could be an AI Jun 18 '20 at 07:14

3 Answers3

0

Once a program is compiled (and debug mode is not used), the name of the variables is lost. So even if you parsed the f-string, you would have no direct way to identify the expected variables.

But it is a real use case that is addressed by pre-compilers: you use special declarations that either depend on macros to save the name of the variables that would be allowed in f-strings, or use a pre-compiler phase that translate a special code into C++ code + do the required special processing (here save the name of the variable in a map). Examples of using macros can be found in Microsoft MFC, and example of pre-compilers can be found in qt or Oracle pro*C.

I am afraid that it will be more complex to develop than the gain for the programmer so my answer boils down to it could be possible, but IMHO is not worth it.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

As Serge Ballista and others have already written, it is (currently) not possible in C++ for cout to resolve the name of a variable to insert its contents. Nevertheless it is possible to use a format string with the number/ position of an argument. With these you can pass the according variables as arguments and refer to them with the number of their position. C++20 defines std::format in the <format> standard library. If you are using an older compiler version, you could use the Boost Format library as in this example:

#include <iostream>
#include <boost/format.hpp>

int main()
{
    std::string a("world");
    std::string b("Hello");

    std::cout << boost::format("%2% %1%!\n") % a % b;
    return 0;
}
Stefan Scheller
  • 953
  • 1
  • 12
  • 22
-1

As many comments points out, there is no such facility for string interpolation. However, if you are trying to make a string with some variable output, you may use sprintf to mimic the string interpolation.

Update : Now this comment is not valid. C++20 now supports the string interpolation as another answer pointed out.

K.R.Park
  • 1,015
  • 1
  • 4
  • 18