Warum wird das Vorlagenparameterpaket beim Funktionsaufruf nicht auf mehrere Typargumente abgeleitet?

Ich habe eine Klasse für einen Typparameter und ein Parameterpaket und bin verwirrt über die Typableitung dieses Typs. Beim Schreiben eines Output-Streaming-Operators habe ich ein Parameterpaket für entdecktoperator<< stimmen nicht sowohl mit den Typ- als auch mit den Paketparametern für die Vorlagenklasse überein:

#include <iostream>

template<class T, class... Ts>
struct foo
{ /* ... */ };

template< class... Ts >
std::ostream& operator<<( std::ostream& os, const foo<Ts...>& )
{
  return os << 42;
}


int main()
{
  std::cout << foo<int>();
}

Dies kann auf gcc-4.7.2 und clang-3.0 nicht kompiliert werden. Ich schätze, ich verstehe die Regeln hier falsch.

gcc sagt (wobei Zeile 16 der Ausgabestream-Aufruf ist):

t.cpp:16:28: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40:0,
                 from t.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ostream:600:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = foo<int>]’

und clang sagt:

t.cpp:16:16: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'foo<int>')
        std::cout << foo<int>();
        ~~~~~~~~~ ^  ~~~~~~~~~~

[--- snip: lots of non-viable candidates from standard library ---]

t.cpp:8:19: note: candidate template ignored: substitution failure [with Ts = <>]
    std::ostream& operator<<( std::ostream& os, const foo<Ts...>& )
                  ^

Könnte mir bitte jemand aufklären, warum das Parameterpaket füroperator<< kann nicht als Typparameter abgeleitet werdenund Parameterpaket fürfoo?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage