mercredi 6 juillet 2016

Construct a std::string with embedded values (e.g. for error messages)?

I want to create a string with embedded information. In C, I would do something like this:

printf("error! value was %d but I expected %d",actualValue,expectedValue)

whereas if I were programming in python, I would do something like this:

"error! value was {0} but I expected {1}".format(actualValue,expectedValue)

How can I do this in C++?

Important Caveats:

  1. I know that I can use std::cout if I want to print such a message to standard output, but I don't want to do that. I want to pass a std::string as an argument to a function (e.g. the constructor of an exception object).

  2. I am using C++11, but portability is potentially an issue, so knowing which methods work and don't work in which versions of C++ would be a plus.

Aucun commentaire:

Enregistrer un commentaire