Imagine this simple constexpr
function:
// Whatever, the exact values don't matter for this example
constexpr float items[100] = { 1.23f, 4.56f };
constexpr int length = 12;
constexpr float getItem(int index)
{
if (index < 0 || index >= length)
{
// ArrayIndexOutOfRangeException has a constructor that takes a const char* and one that takes a std::string.
throw ArrayIndexOutOfRangeException("You did a bad.");
}
return items[index];
}
You can use it like this:
int main()
{
constexpr float f1 = getItem( 0); std::cout << f1 << std::endl; // Works fine
constexpr float f2 = getItem( 1); std::cout << f2 << std::endl; // Works fine
constexpr float f3 = getItem(-1); std::cout << f3 << std::endl; // Does not compile: "constexpr variable 'f3' must be initialized by a constant expression", "subexpression not valid in a constant expression"
constexpr float f4 = getItem(20); std::cout << f4 << std::endl; // Does not compile: "constexpr variable 'f4' must be initialized by a constant expression", "subexpression not valid in a constant expression"
return 0;
}
Great! BUTT WEIGHT!
volatile int i;
i = 123; // As a placeholder for something like this: std::cin >> i;
float f5 = getItem(i);
std::cout << f5 << std::endl;
This throws at runtime with "terminate called after throwing an instance of 'ArrayIndexOutOfRangeException'" and "what(): You did a bad."
Ok, that's not very helpful and I want to create a better error message:
constexpr float getItem(int index)
{
if (index < 0 || index >= length)
{
std::stringstream stream;
stream << "You did a bad. getItem was called with an invalid index (" << index << "), but it should have been non-negative and less than the total number of items (" << length << ").";
throw ArrayIndexOutOfRangeException(stream.str());
}
return items[index];
}
But that's not allowed: "variable of non-literal type 'std::stringstream' (aka 'basic_stringstream') cannot be defined in a constexpr function".
I'd be OK with having a simpler error message for the compile-time version and only do the complicated string manipulation in the run-time version.
So... How?
Please note that this is C 17 (some GCC flavor), which might not have some constexpr-related features that C 20 would have.
Other code requires that the function stays constexpr
.
I would like to avoid duplicating the function, if at all possible.
CodePudding user response:
You might move the error message creation in another function:
std::string get_error_message(int index, int length = 10)
{
std::stringstream stream;
stream << "You did a bad. getItem was called with an invalid index ("
<< index
<< "), but it should have been non-negative "
<< "and less than the total number of items ("
<< length << ").";
return stream.str();
}
constexpr float getItem(int index)
{
constexpr int length = 10;
constexpr std::array<float, length> items{};
if (index < 0 || index >= length)
{
throw std::runtime_error(get_error_message(index, length));
}
return items[index];
}