2012-06-17 6 views
6

Jeśli jest to możliwe, można indeksować do pakietu parametrów variadic bez rekursji. Jednak GCC jest refusing to pick up my partial specialization tutaj:Czy mogę częściowo wyspecjalizować szablon z wzorem takim jak foo <T ..., int, U ...>?

template <int I, typename List> 
struct element_impl; 

template <typename... TL, int... IL, typename T, int I, typename... TR, int... IR> 
struct element_impl<I, typelist<pair<TL,IL>..., pair<T,I>, pair<TR,IR>...>> { 
    typedef T type; 
}; 

prog.cpp: In instantiation of ' element<0, typelist<int, double, char, float, long int> > ':
prog.cpp:52:34: instantiated from here
prog.cpp:47:79: error: invalid use of incomplete type ' struct element_impl<0, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > '

Czy GCC buggy, albo ja ignorując pewne ograniczenia o zmiennej liczbie argumentów szablonów?

+0

Dzięki [temu prostemu kodowi] (http://ideone.com/CrNSc) GCC mówi: 'error: pakiet parametrów 'T' musi znajdować się na końcu listy parametrów szablonu'. Dlatego uważam, że jest to ograniczenie językowe. – Nawaz

+0

@Nawaz To wcale nie jest to samo. –

+0

To nie jest "dokładnie" to samo, ale komunikat o błędzie jest całkiem jasny: pakiet parametrów * musi być * na końcu listy parametrów szablonu. GCC powtarza tę samą wiadomość: http://ideone.com/2Rifn – Nawaz

Odpowiedz

5

Spec mówi w 14.8.2.5p9

If P has a form that contains <T> or <i> , then each argument Pi of the respective template argument list P is compared with the corresponding argument Ai of the corresponding template argument list of A . If the template argument list of P contains a pack expansion that is not the last template argument, the entire template argument list is a non-deduced context.

Twój typelist<T> niestety pasuje ten wzorzec.

+0

Czy możesz rozwinąć tekst Standardu prostymi słowami, być może z jednym lub dwoma przykładami? – Nawaz

+0

@Nawaz @RMartinho pokazał doskonały przykład ta reguła, prawdopodobnie lepsza niż wszystko, co mogłem wymyślić. "" oznacza "lista szablonów argumentów zawierająca parametr typu (pakiet)" i "" oznacza "listę argumentów szablonu zawierającą parametr nie typu (pakiet)". –

2

AFAICT, zasady dopasowywania częściowych specjalizacji są takie same, jak w regułach, odliczanie parametrów funkcji. I §14.8.2.1/1 mówi, co następuje:

For a function parameter pack that occurs at the end of the parameter-declaration-list, the type A of each remaining argument of the call is compared with the type P of the declarator-id of the function parameter pack. Each comparison deduces template arguments for subsequent positions in the template parameter packs expanded by the function parameter pack. For a function parameter pack that does not occur at the end of the parameter-declaration-list, the type of the parameter pack is a non-deduced context.

Więc paczki TL i IL nie można wywnioskować, w tym przypadku, a częściowa specjalizacja nie jest zrywane.

Powiązane problemy