標籤:
在以前的博文中,寫過類似的課題。使用的是下面這種方法。
// 遞迴出口template <typename T>void logOld(const T& t){ std::cout << t << ‘\n‘;}// 遞迴展開template <typename T, typename ... Args>void logOld(const T& t, const Args& ... args){ std::cout << t << ‘ ‘; logOld(args...);}
函數調用有遞迴,模板在展開的時候也可以有遞迴。在寫遞迴函式時,我們需要找出遞迴的出口,這麼不會陷入無限遞迴。同樣,在運用模板展開時候遞迴同樣需要考慮遞迴的出口。
為了實現隨意log的輸出,比如
zz::logOld(1); zz::logOld(1, 2); zz::logOld(1, 2, ‘-‘, "Today"); int a[4]; zz::logOld("Hello", "World", 2016, ‘-‘, 3, ‘-‘, 19, a);
那麼參數就是不可控的,但是在遞迴展開中,最後一個參數,肯定是遞迴的出口,如上面logOld函數定義所示。啟動並執行效果:
但是,這種是遞迴實現,產生的程式碼也是嵌套的,效能方面有所損失,但是現實起來方便也很容易理解。
最近看了一些文章,關於可變參數模板的,有一些關於Parameter pack的文章,其中有關於Pack expansion的使用。
>
Pack expansio
A pattern followed by an ellipsis, in which the name of at least one parameter pack appears at least once, is expanded into zero or more comma-separated instantiations of the pattern, where the name of the parameter pack is replaced by each of the types from the pack, in order.
其就是提供一種多參數解包的模式,模式必須含有至少一個參數。
根據這個特性,我設計了以下的代碼
template <typename T>typename std::decay<T> unpack(const T& t){ std::cout << t << ‘ ‘; typename std::decay<T> _ins; return _ins;}template <typename T, typename ... Args>void debugLogImpl(const T& t, const Args& ... args){ std::cout << t << ‘ ‘; auto _fun = [](...) {}; _fun(unpack(args)...); std::cout << ‘\n‘;}
unpack為解包一個參數的模式,unpack(args)…介紹多參數。
比如args為a,b,c,那麼unpack(args)…等價於unpack(a), unpack(b), unpack(c)。這種形式的解包結果,我現在只想到了作為函數參數。所有我定義了一個lambda函數,其接受多參數,然後調用這個lambda函數來調用解包之後的unpack函數集合。
但是函數參數入棧的順序是從右往左,那麼先調用的是unpack(c),輸出的也就是”c”,那麼不行的。但是在C++14中我們可以直接
(unpack(args), ...);
但是在C++11中,這是不可以的。我也沒有找出這麼順序調用解包之後的函數的辦法。為瞭解決這個log順利不對的問題,無奈又寫了一大段的特例模板。
template <typename T>typename std::decay<T> unpack(const T& t){ std::cout << t << ‘ ‘; typename std::decay<T> _ins; return _ins;}template <typename T, typename ... Args>void debugLogImpl(const T& t, const Args& ... args){ std::cout << t << ‘ ‘; auto _fun = [](...) {}; _fun(unpack(args)...); std::cout << ‘\n‘;}template <typename T0>void debugLog(const T0& t0){ debugLogImpl(t0);}template <typename T0, typename T1>void debugLog(const T0& t0, const T1& t1){ debugLogImpl(t0, t1);}template <typename T0, typename T1, typename T2>void debugLog(const T0& t0, const T1& t1, const T2& t2){ debugLogImpl(t0, t2, t1);}template <typename T0, typename T1, typename T2, typename T3>void debugLog(const T0& t0, const T1& t1, const T2& t2, const T3& t3){ debugLogImpl(t0, t3, t2, t1);}...template <typename T0, typename T1, typename T2, typename T3, typename T4 , typename T5, typename T6, typename T7, typename T8, typename T9>void debugLog(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4 , const T5& t5 ,const T6& t6, const T7& t7, const T8& t8, const T9& t9){ debugLogImpl(t0, t9, t8, t7, t6, t5, t4, t3, t2, t1);}
將參數調個個。
#define DEBUG_LOG(...) zz::debugLog(__VA_ARGS__)
定義一個宏,來統一對外口徑。
int main(){// zz::logOld(1);// zz::logOld(1, 2);// zz::logOld(1, 2, ‘-‘, "Today");// int a[4];// zz::logOld("Hello", "World", 2016, ‘-‘, 3, ‘-‘, 19, a); DEBUG_LOG(1); DEBUG_LOG(1, 2); DEBUG_LOG(1, 2, 3); DEBUG_LOG(1, ‘-‘, ‘a‘, 1, "sssss"); return 0;}
結果如下
然後就是
template <typename T>typename std::decay<T> unpack(const T& t){ std::cout << t << ‘ ‘; typename std::decay<T> _ins; return _ins;}
這邊為什麼要用decay,如果不用向這樣
template <typename T>T unpack(const T& t){ std::cout << t << ‘ ‘; return t;}
對於這樣的“ssssss”,其會報
/home/zhou/work/qt_project/zzLog/main.cpp:48: error: no matching function for call to ‘unpack(const char [6])‘ _fun(unpack(args)...);---/home/zhou/work/qt_project/zzLog/main.cpp:36: error: function returning an array ^
使用decay將數組退化為const char*來解決問題。
利用可變模板參數實現log功能