本文介绍了c ++ 11递归可变模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解递归变量模板如何工作。

I am trying to understand how recursive variadic templates work.

#include <iostream>

template<typename T>
static inline void WriteLog(T&& msg) {
    std::wcout << std::forward<T>(msg);
}

template<typename T, typename... Ts>
static void WriteLog(T&& msg, Ts&&... Vals) {
    WriteLog(std::forward<T>(msg));
    WriteLog(std::forward<Ts>(Vals)...);
    std::wcout << "\n**End**";
}

int main() {
    WriteLog("apple, ", "orange, ", "mango");
}

输出:

apple, orange, mango
**End**
**End**

我期望只有一个 **结束** 。为什么会打印两次?

I expected only one **End**. Why is it printed twice?

推荐答案

调用树:

 WriteLog("apple, ", "orange, ", "mango");
       ->WriteLog("apple, ");
            -> std::wcout << "apple, ";
       ->WriteLog( "orange, ", "mango");
            ->WriteLog("orange, ");
                 -> std::wcout << "orange, ";
            ->WriteLog( "mango");
                 -> std::wcout << "mango";
            ->std::wcout << "\n**End**";
       ->std::wcout << "\n**End**";

这篇关于c ++ 11递归可变模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 21:50