这是C++,尽量避免使用MACRO。通常有一些constexpr方法是可能的,比如这样:
#include <string_view>
#include <vector>
#include <iostream>
#include <format>
struct ExitCode
{
int code;
std::string_view name;
std::string_view description;
// implicit conversion to int (for interop with other API's)
operator int() const noexcept
{
return code;
}
};
std::ostream& operator<<(std::ostream& os, const ExitCode& err)
{
os << std::format("Error : {}, description = {}", err.name, err.description);
return os;
}
//-----------------------------------------------------------------------------
static constexpr ExitCode EXITCODE_SOMENAME{123,"SOMENAME","Description of code"};
// return a ref, all data will be put in by compiler (no dynamic mem alloc)
const ExitCode& SomeFunction()
{
return EXITCODE_SOMENAME;
}
int main()
{
auto& retval = SomeFunction();
std::cout << retval << "\n";
return retval; // implicit conversion to int here
}