You can only define a variable inside an if's () if the entire contents of the () starts with the variable declaration (ie, if it is nothing but a variable declaration).
What you are trying to do is both declare a variable, and then do a test on it. That isn't allowed.
You can either fall back to the two line version, or you can write up a container-based find that uses boost::optional or std::tr2::optional as follows:
namespace aux {
using std::begin; using std::end;
template<typename C> auto adl_begin( C&& c )->decltype( begin(std::forward<C>(c)) )
{ return begin(std::forward<C>(c)); }
template<typename C> auto adl_end( C&& c )->decltype( end(std::forward<C>(c)) )
{ return end(std::forward<C>(c)); }
}
using aux::adl_begin; using aux::adl_end;
template<typename C, typename U>
optional< decltype( typename std::decay<*adl_begin( std::declval<C&>() )>::type ) >
my_find( C&& c, U&& u ) {
auto it = std::find( adl_begin(c), adl_end(c), std::forward<U>(u) );
if (it == adl_end(c))
return {none_t};
else
return {it};
}
Instead of returning an iterator, the above returns an optional iterator, which when evaluated in a boolean context is false if the item isn't found.
You can now type:
if( auto op_it = my_find( v, 2 ) ) {
auto it = *op_it; // optional, but saves on `*` elsewhere
// code
}
and get roughly what you want.
optional is available in boost, in maybe std::tr2, and probably in std:: in C++14. The boost and std ones are slightly different.