ich dieses Beispiel haben:Operator << auf Template-Argument Typ verursacht Mitglied Fehler nur in Klirren
#include <iostream>
#include <tuple>
#include <string>
template <typename T>
class A {
public:
A(const T &t) : m_t(t) {}
void foo() {
std::cout << m_t << std::endl;
}
private:
T m_t;
};
typedef std::tuple<std::string, std::string> Type;
std::ostream &operator<<(std::ostream &os, const Type &t) {
os << std::get<0>(t) << " " << std::get<1>(t);
return os;
}
int main() {
A<Type> a(Type{"ala", " ma kota"});
a.foo();
return 0;
}
, die mit Klappern ++ (3.6) erzeugt:
test_clang.cpp:10:19: error: call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup
std::cout << m_t << std::endl;
^
test_clang.cpp:26:7: note: in instantiation of member function 'A<std::tuple<std::basic_string<char>, std::basic_string<char> > >::foo' requested here
a.foo();
^
test_clang.cpp:19:15: note: 'operator<<' should be declared prior to the call site
std::ostream &operator<<(std::ostream &os, const Type &t) {
Keine Fehler bei g ++ aufgetreten - 4.8 mit C++ 11 und g ++ - 5.2.1 mit C++ 17 Builds. clang ++ - 3.6 muss std::ostream &operator<<(std::ostream &os, const Type &t)
definiert werden vorA::foo<T>
.
Aus meiner Perspektive Mitglied m_t
hängt von Vorlage Argumenttyp und verwendet operator<<
für diesen Typ sollte nicht während der Template-Definition benötigt werden. Warum hat call compilation error und g ++ nicht?
Es scheint, dass 14.7.3 [temp.expl.spec] Absatz 7 (vor allem der letzte Satz) gilt. –
@ DietmarKühl aber hier gibt es keine explizite Spezialisierung, oder? – TartanLlama