I would like to store all error messages in one file and call them for example with

if (!std::filesystem::exists(ArgFilename)) {
        throw std::runtime_error{error_001_nofile};
    } else {
//do something

The file errorcodes.h is included to the main program and looks like

#include <string_view>

constexpr char error_001_nofile[] = "Error 001: File not found.";
constexpr char error_002_nomem[] = "Error 002: Not enough memory.";
#endif /* SRC_ERRORCODES_H_ */

Cevelop warns, that constexpr char is no proper c++, but c code. Is there a clean c++ solution to manage error messages?

Cevelop warns, that constexpr char is no proper c++, but c code.

  • 1
    \$\begingroup\$ const should be enough if there's nothing to evaluate. \$\endgroup\$ – πάντα ῥεῖ Nov 14 '19 at 21:46
  • \$\begingroup\$ Why did someone down vote? What can I improve in the question? \$\endgroup\$ – Jonas Stein Nov 14 '19 at 22:25
  • \$\begingroup\$ @πάνταῥεῖ Thank you. Will change that here, but I leave the question as is for the reader. \$\endgroup\$ – Jonas Stein Nov 14 '19 at 22:26
  • \$\begingroup\$ There's nothing wrong with using C-string constants in C++. I would write char const* name = "value";, but I think in the end it's the same. \$\endgroup\$ – Cris Luengo Nov 14 '19 at 23:29

constexpr is probably overkill, but not in any way wrong.

You might consider using the C++ universal style initializisation:

constexpr char error_001_nofile[]{"Error 001: File not found."};

or using a std::string:

constexpr std::string error_001_nofile{"Error 001: File not found."};

Not the answer you're looking for? Browse other questions tagged or ask your own question.