Is it possible to use template types from inline function in lambdas in C ++ 11?

So, I have some code similar to this one:

(Note: not actual code, just shorthand from a somewhat lengthy and complex method for brevity.)

template<typename ArgT0, typename ArgT1, typename FuncT>
static void addMethod( const std::string& name, FuncT func )
{
   Method script_func = [&]( const Arguments& args ) -> Value
   {
      func(UnsafeAnyCast<ArgT0>(args[0]),UnsafeAnyCast<ArgT1>(args[1]));
      return Value::Undefined();
   }

   _prototype->Set( name, script_func );
}

      

It works great in Visual Studio 2010, but I know that this is far from any guarantee that it is C ++ compatible. Is there something wrong with this to do with using template arguments inside a lambda?

+3


source to share


1 answer


Yes, this is standard compliant: the lambda expression has access to all visible names in its enclosing scope, these are only variables to be captured

5.1.2 Lambda Expressions [expr.prim.lambda]



9 A lambda expression whose smallest enclosing area is block scope (3.3.3) is a local lambda expression; any other lambda expression will not have a capture list in its lambda introducer. Reaching the scope of a local lambda expression is a set of enclosing scopes upward including both the innermost enclosing function and its parameters

+1


source







All Articles