I want to create some template, which essentially should wrap it with a parameter. The parameter must be an arbitrary function call, which is wrapped using a magic metaprogramming template with a prefix and a postfix code.
I want to use it as follows:
auto result = try_call( some_vector.at(13) );
and try_callwill be determined in some way that it completes the try..catch block around some_vector.at(13). Something like that:
template<typename T>
try {
auto value =
return std::experimental::optional<T>(value);
}
catch (std::exception&) {
return std::experimental::nullopt;
}
There is an article by Bjarne Straustrup, but this is not an exact description of what I need, and I could not find a solution to this problem.
If this is not possible directly, I am currently thinking about it through a template function taking a lambda:
template<typename Func>
auto try_call(Func f) {
try {
return f();
} catch(std::exception&) {
return std::experimental::nullopt;
}
}
, . , ? .