代码之家  ›  专栏  ›  技术社区  ›  Akira

C++14中使用泛型lambda函数的递归

  •  2
  • Akira  · 技术社区  · 8 年前

    this 问题是 answer 详细介绍了如何使用通用lambda函数替换 std::function technique

    基于以上内容,我创建了以下工作示例:

    #include <cstdio>
    
    void printSeq(unsigned start) {
        auto recursion = [](auto&& self, const char* format, unsigned current) {
            printf(format, current);
            if(!current)
                return static_cast<void>(printf("\n"));
            else
                self(self, ", %u", current - 1);
        };
    
        recursion(recursion, "%u", start);
    }
    
    int main() {
        return (printSeq(15), 0);
    }
    

    我的问题是,使用 auto&& 超过 auto& std::move 在这里

    1 回复  |  直到 8 年前
        1
  •  3
  •   Yakk - Adam Nevraumont    8 年前

    auto& 仅为左值。

    例如,在重构并用临时代理记忆器替换左值递归对象之前,这无关紧要。

    auto&& 自动(&A); 声明“不允许临时工!”有时你想在引用时排除临时词,但这种情况很少见。

    auto const& , auto 自动(&A)&

    自动(&A); 您可以排除代理引用。