简体   繁体   中英

c++11 recursive variadic templates

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");
}

Output:

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

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

the call tree :

 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**";

When the recursive call to WriteLog(std::forward<Ts>(Vals)...); is finished, it has to execute the next statement. This function is called twice (once for "apple" and once for "orange" ) and thus two print outs of "**End**" are written.

The last recursive call for "mango" goes straight to the first overload since there is only a single argument left in the pack.

**End** is printed for the calls of

  • WriteLog("apple, ", "orange, ", "mango"); in main
  • WriteLog("orange, ", "mango"); (with line WriteLog(std::forward<Ts>(Vals)...); ) in WriteLog("apple, ", "orange, ", "mango")

I'll be honest with you, I've been writing c++11 template code for 4 years and I still have trouble remembering how to match the empty argument pack...

this little trick avoids recursive template expansion altogether: (EDIT: re-written to support zero arguments and automatic comma separator insertion)

#include <iostream>

namespace detail {
    struct writer
    {
        template<class T>
        void operator()(const T& t) {
            if (_first) {
                _first = false;
            }
            else {
                std::cout << ", ";
            }
            std::cout << t;
        }

    private:
        bool _first = true;
    };

    // non-template overload to catch no-parameter case
    void do_write(writer&&)
    {

    }

    // general case. Note w is passed by r-value reference
    // to allow the caller to construct it in-place        
    template<typename T, typename...Ts>
    void do_write(writer&& w, const T& t, Ts&&...ts)
    {
        w(t);
        do_write(std::forward<writer>(w), std::forward<Ts>(ts)...);
    }


}

// method 1 - no recursion
template<typename... Ts>
void WriteLog1(Ts&&... Vals) {
    // expand one call for each parameter
    // use comma operator to ensure expression result is an int
    detail::writer write;

    using expander = int[];
    expander { 0, (write(std::forward<Ts>(Vals)), 0)... };

    // write the final linefeed
    std::cout << std::endl;
}

// method 2 - recursion

template<typename...Ts>
void WriteLog2(Ts&&...ts)
{
    detail::do_write(detail::writer(), std::forward<Ts>(ts)...);
    std::cout << std::endl;
}

int main() {
    WriteLog1("apple", "orange", "mango");
    WriteLog1("apple", "orange");
    WriteLog1("apple");
    WriteLog1("apple", 1.0, "orange", 1L, "mango", 2.6f);
    WriteLog1(); // test pathalogical case

    WriteLog2("apple", "orange", "mango");
    WriteLog2("apple", "orange");
    WriteLog2("apple");
    WriteLog2("apple", 1.0, "orange", 1L, "mango", 2.6f);
    WriteLog2(); // test pathalogical case
    return 0;
}

output:

apple, orange, mango                                                                                                                                             
apple, orange                                                                                                                                                    
apple                                                                                                                                                            
apple, 1, orange, 1, mango, 2.6                                                                                                                                  

apple, orange, mango                                                                                                                                             
apple, orange                                                                                                                                                    
apple                                                                                                                                                            
apple, 1, orange, 1, mango, 2.6

>                                                                                                                                  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM