【问题标题】:template in lambda closurelambda 闭包中的模板
【发布时间】:2016-02-16 16:57:08
【问题描述】:

谁能告诉我为什么下面的代码

namespace detail
{
    ...

    template<typename ... T_Args>
    void duk_get_args(duk_context*& context,  std::function<void(T_Args...)>& func)
    {

        duk_get_args_impl<T_Args ...>(context, func, std::index_sequence_for<T_Args ...>());
    }
}
template<typename ... T_Args>
duk_c_function duk_function(std::function<void(T_Args ...)> function_item)
{
    std::function<void(T_Args ...)> closure_function = function_item;
    duk_c_function function_return = [closure_function] (duk_context* ctx) -> duk_ret_t {
        detail::duk_get_args<T_Args ...>(ctx, closure_function);
        return 0;
    };
    return function_return;
}

抛出以下错误,是否有解决方法?

||=== Build: Release_win64 in dukbind (compiler: GNU GCC Compiler) ===|
include\dukbind\detail.hpp||In instantiation of 'dukbind::duk_function(std::function<void(T_Args ...)>)::<lambda(duk_context*)> [with T_Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}; duk_ret_t = int; duk_context = duk_hthread]':|
include\dukbind\detail.hpp|81|required from 'struct dukbind::duk_function(std::function<void(T_Args ...)>) [with T_Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}; dukbind::duk_c_function = std::function<int(duk_hthread*)>]::<lambda(duk_context*)>'|
include\dukbind\detail.hpp|85|required from 'dukbind::duk_c_function dukbind::duk_function(std::function<void(T_Args ...)>) [with T_Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}; dukbind::duk_c_function = std::function<int(duk_hthread*)>]'|
C:\Users\Daniel Wanner\Documents\Projekte\C++\dukbind\src\main.cpp|166|required from here|
include\dukbind\detail.hpp|83|error: no matching function for call to 'duk_get_args(duk_context*&, const std::function<void(std::__cxx11::basic_string<char>)>&)'|
include\dukbind\detail.hpp|70|note: candidate: template<class ... T_Args> void dukbind::detail::duk_get_args(duk_context*&, std::function<void(T_Args ...)>&)|
include\dukbind\detail.hpp|70|note:   template argument deduction/substitution failed:|
include\dukbind\detail.hpp|83|note:   types 'std::function<void(T_Args ...)>' and 'const std::function<void(std::__cxx11::basic_string<char>)>' have incompatible cv-qualifiers|
C:\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\5.1.0\include\c++\functional|2250|error: 'std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = dukbind::duk_function(std::function<void(T_Args ...)>) [with T_Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}; dukbind::duk_c_function = std::function<int(duk_hthread*)>]::<lambda(duk_context*)>; <template-parameter-2-2> = void; _Res = int; _ArgTypes = {duk_hthread*}]', declared using local type 'dukbind::duk_function(std::function<void(T_Args ...)>) [with T_Args = {std::__cxx11::basic_|
||=== Build failed: 2 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|

对于几乎 90% 的代码问题,我很抱歉,但我相信我的概念有问题。我试图在 lambda 闭包中使用模板参数。这在概念上是否可行?

【问题讨论】:

    标签: c++ templates lambda


    【解决方案1】:

    Lambda 函数有一个operator(),默认标记为const,这意味着捕获的closure_functionconst。但是,duk_get_args 试图将非const 引用引用到const 参数,这就是编译器所抱怨的。

    要么:

    1. duk_get_args 更改为const func
    2. Add mutable to the lambda's definition,因此您可以将非const 引用引用到closure_function

    另外,closure_function 变量是不必要的。您可以捕获 function_item 变量并使用它。这不是你的问题的一部分,但我觉得要指出一点。

    【讨论】:

    • 非常感谢。似乎这就是问题所在。我不知道 lambdas 将变量捕获为 const 引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多