<tfoot id='FrASk'></tfoot>

  1. <small id='FrASk'></small><noframes id='FrASk'>

    <i id='FrASk'><tr id='FrASk'><dt id='FrASk'><q id='FrASk'><span id='FrASk'><b id='FrASk'><form id='FrASk'><ins id='FrASk'></ins><ul id='FrASk'></ul><sub id='FrASk'></sub></form><legend id='FrASk'></legend><bdo id='FrASk'><pre id='FrASk'><center id='FrASk'></center></pre></bdo></b><th id='FrASk'></th></span></q></dt></tr></i><div id='FrASk'><tfoot id='FrASk'></tfoot><dl id='FrASk'><fieldset id='FrASk'></fieldset></dl></div>
    1. <legend id='FrASk'><style id='FrASk'><dir id='FrASk'><q id='FrASk'></q></dir></style></legend>
        <bdo id='FrASk'></bdo><ul id='FrASk'></ul>

      如何使用 lambda 表达式作为模板参数?

      时间:2023-05-24

          <tfoot id='TVE6H'></tfoot>
            • <bdo id='TVE6H'></bdo><ul id='TVE6H'></ul>

                <tbody id='TVE6H'></tbody>

              <small id='TVE6H'></small><noframes id='TVE6H'>

              • <i id='TVE6H'><tr id='TVE6H'><dt id='TVE6H'><q id='TVE6H'><span id='TVE6H'><b id='TVE6H'><form id='TVE6H'><ins id='TVE6H'></ins><ul id='TVE6H'></ul><sub id='TVE6H'></sub></form><legend id='TVE6H'></legend><bdo id='TVE6H'><pre id='TVE6H'><center id='TVE6H'></center></pre></bdo></b><th id='TVE6H'></th></span></q></dt></tr></i><div id='TVE6H'><tfoot id='TVE6H'></tfoot><dl id='TVE6H'><fieldset id='TVE6H'></fieldset></dl></div>

              • <legend id='TVE6H'><style id='TVE6H'><dir id='TVE6H'><q id='TVE6H'></q></dir></style></legend>
                本文介绍了如何使用 lambda 表达式作为模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                如何使用 lambda 表达式作为模板参数?例如.作为初始化 std::set 的比较类.

                How to use lambda expression as a template parameter? E.g. as a comparison class initializing a std::set.

                以下解决方案应该有效,因为 lambda 表达式仅创建一个匿名结构,它应该适合作为模板参数.然而,产生了很多错误.

                The following solution should work, as lambda expression merely creates an anonymous struct, which should be appropriate as a template parameter. However, a lot of errors are spawned.

                代码示例:

                struct A {int x; int y;};
                std::set <A, [](const A lhs, const A &rhs) ->bool {
                    return lhs.x < rhs.x;
                    } > SetOfA;
                

                错误输出(我使用的是 g++ 4.5.1 编译器和 --std=c++0x 编译标志):

                Error output (I am using g++ 4.5.1 compiler and --std=c++0x compilation flag):

                error: ‘lhs’ cannot appear in a constant-expression
                error: ‘.’ cannot appear in a constant-expression
                error: ‘rhs’ cannot appear in a constant-expression
                error: ‘.’ cannot appear in a constant-expression
                At global scope:
                error: template argument 2 is invalid
                

                这是预期的行为还是 GCC 中的错误?

                Is that the expected behavior or a bug in GCC?

                编辑

                正如有人指出的那样,我错误地使用了 lambda 表达式,因为它们返回了他们所指的匿名结构的实例.

                As someone pointed out, I'm using lambda expressions incorrectly as they return an instance of the anonymous struct they are referring to.

                但是,修复该错误并不能解决问题.对于以下代码,我在未评估的上下文中收到 lambda-expression 错误:

                However, fixing that error does not solve the problem. I get lambda-expression in unevaluated context error for the following code:

                struct A {int x; int y;};
                typedef decltype ([](const A lhs, const A &rhs) ->bool {
                    return lhs.x < rhs.x;
                    }) Comp;
                std::set <A, Comp > SetOfA;
                

                推荐答案

                std::set 的第二个模板参数需要 type,而不是 表达式,所以只是你用错了.

                The 2nd template parameter of std::set expects a type, not an expression, so it is just you are using it wrongly.

                您可以像这样创建集合:

                You could create the set like this:

                auto comp = [](const A& lhs, const A& rhs) -> bool { return lhs.x < rhs.x; };
                auto SetOfA = std::set <A, decltype(comp)> (comp);
                

                这篇关于如何使用 lambda 表达式作为模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:返回转换容器的 std::transform-like 函数 下一篇:编译时模板实例化检查

                相关文章

                最新文章

                  <tfoot id='NYrbP'></tfoot>

                  • <bdo id='NYrbP'></bdo><ul id='NYrbP'></ul>

                    <i id='NYrbP'><tr id='NYrbP'><dt id='NYrbP'><q id='NYrbP'><span id='NYrbP'><b id='NYrbP'><form id='NYrbP'><ins id='NYrbP'></ins><ul id='NYrbP'></ul><sub id='NYrbP'></sub></form><legend id='NYrbP'></legend><bdo id='NYrbP'><pre id='NYrbP'><center id='NYrbP'></center></pre></bdo></b><th id='NYrbP'></th></span></q></dt></tr></i><div id='NYrbP'><tfoot id='NYrbP'></tfoot><dl id='NYrbP'><fieldset id='NYrbP'></fieldset></dl></div>
                  1. <small id='NYrbP'></small><noframes id='NYrbP'>

                    <legend id='NYrbP'><style id='NYrbP'><dir id='NYrbP'><q id='NYrbP'></q></dir></style></legend>