2013-07-15 77 views
3

爲什麼參考塌陷不參考與模板模板類崩

template<typename T, template<typename> class C> 
void f(C<T> && x); // x declaration is an rvalue! 

申請我怎麼能完全向前,我怎麼能避免超載常量左值裁判,左值裁判,右值裁判的所有組合中

template<typename T> // not necessary a template template class here 
void f(C<T>, C<T>, C<T>, ..., C<T>) 
{ 
    // do something with T 
    // move or copy arguments to a function 
} 
+4

參考摺疊僅適用於格式爲「T &&」的情況。 ''C &&''總是指定一個右值引用(如'std :: vector &&') –

+2

@AndyProwl:或者當表單是'auto &&'時。 – Nawaz

+0

@AndyProwl是的,但沒有崩潰我無法解決第二個問題! –

回答

2

您將定義使用某種SFINAE,可惜

template<typename T> 
struct HasOneTypeParam : std::false_type { }; 

template<typename T, template<typename> class C> 
struct HasOneTypeParam<C<T>> : std::true_type { }; 

template<typename ...T> 
struct SlurpThemAll { typedef int type; }; 

template<typename ...T, 
    typename SlurpThemAll< 
    bool[HasOneTypeParam<typename std::decay<T>::type>::value * 2 - 1]... 
    >::type = 0> 
void f(T &&... x); 

依靠根據你真正想要做的事情,你可以要求所有的std::decay<T>::type與更多的SFINAE黑客相同。