c++ - 在 C++11 的 lambda 中使用封闭函数中的模板类型可以吗?

标签 c++ templates c++11 lambda

所以我目前有一些类似的代码:

(注意:不是实际代码,只是为了简洁起见从一个有点长且复杂的方法中删减。)

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/

相关文章:

带有 VARIANT/bstrVal 代码的 C++ 泄漏

c++ - 如何为相同类型的 typedef 提供模板特化?

c++ - 模板类中的 Int 模板成员函数

C++11 - typeid 唯一性

c++ - 使用表复制指向对象的指针

c++ - 在 .Net 中使用 OpenSSL RSA key

c++ - 二进制搜索 - 代码编译和运行后不显示输出

c++ - 哪个算法需要 "visitor"(boost 库中的术语)?

c++ - Const 限定符和前向引用

c++ - 为什么不允许 double 作为非类型模板参数?