As an example, say I have the class
class A{
B& foo;
};
and I want to initialize this class with a constructor that takes in a vector and an index (just for example).
So I get
explicit A(std::vector<B>& lst, int index):
foo(lst[index])
{};
However, accessing a random index in the vector is unsafe, since it would be UB if it was out of bounds. So, I throw an exception
explicit A(std::vector<B>& lst, int index):
foo(lst[index])
{
if (index >= lst.size()){
//throw blah blah
}
};
However, this doesn't actually save me at all, because the member initializer list is ran before the constructor body. So the possible out of bounds access would happen first, and only after that the check runs.
My current thoughts to solve this are:
Assign values in the constructor body.
Make this check happen before calling the constructor
Are there any language abilities that let me throw an exception early? And if not, what is standard practice for this situation.
Besides the solutions in the comments, you can use the ternary operator:
explicit A(std::vector<B>& lst, int index):
foo(lst[index < lst.size() ? index : throw blah_blah()])
{}
It requires no helper functions and allows customizing your throw
.