所以我目前有一些类似的代码:
(注意:不是实际代码,只是为了简洁起见从一个有点长且复杂的方法中删减。)
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 );
}
它在 Visual Studio 2010 中运行良好,但我知道这远不能保证它是符合标准的 C++。就在 lambda 中使用模板参数而言,这有什么问题吗?
最佳答案
是的,这是符合标准的:lambda 表达式可以访问其封闭范围内的所有可见名称,它只是您需要捕获的变量
5.1.2 Lambda 表达式 [expr.prim.lambda]
9 A lambda-expression whose smallest enclosing scope is a block scope (3.3.3) is a local lambda expression; any other lambda-expression shall not have a capture-list in its lambda-introducer. The reaching scope of a local lambda expression is the set of enclosing scopes up to and including the innermost enclosing function and its parameters
关于c++ - 在 C++11 的 lambda 中使用封闭函数中的模板类型可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14639401/