When wrapping initializations of a constant I frequently run into scope issues
try {
const int value = might_throw();
}
std::cout << value << "\n"; /* error, value out of scope */
Currently I use a temporary value as a workaround. Is there a better way to deal with const – try {} situations?
int tmp; /* I'd rather have tmp const */
try {
tmp = might_throw();
}
catch (...) {
/* do something */
}
const int value = tmp;
Instead of your
you can do this:
Or, in C++11 you can do