嘗試
template<template<typename...> class C1, template<typename...> class C2, typename T, typename... Args>
C1<C2<T, Args...>> f (const C2<T, Args...>& v, int size)
{...}
,以允許不同的容器或
template<template<typename...> class C, typename T, typename... Args>
C<C<T, Args...>> f (const C<T, Args...>& v, int size)
{...}
如果內部和外部容器需要是相同的或也許僅僅是:
template<template<typename...> class C, typename T>
C<T> f (const T& v, int size)
和訪問T::value_type
代替上述第一個例子中的T
。
很難提供更好的諮詢爲一體的用例是從你的問題不清楚。
更新:解釋爲什麼你的企圖沒有工作:
template<template<typename U, typename Alloc> class C, typename T>
C<C<T, Alloc>, Alloc> f (const C<T, Alloc>& v, int size)
{...}
看看它和再想一想:首先,你需要Alloc
作爲另一個參數作爲參數的名稱模板模板參數不適用於任何事情。這將導致:
template<template<typename T_dummy, typename A_dummy> class C, typename T, typename Alloc>
C<C<T, Alloc>, Alloc> f (const C<T, Alloc>& v, int size)
{...}
更好,但現在你有一個真正的問題:內部容器的分配器是一樣的東西
std::allocator<int>
,適用於內的容器,而不是爲外部一個需要的東西在線
std::allocator<std::vector<int>>
因此,嘗試重新使用內部分配器的外部容器註定要失敗。
哪'Alloc'你想爲你的返回容器(外一種)? – Jarod42