Template function matching
Verfasst: 15.06.2012, 13:54
Ich versuche mich mal wieder in der Welt der Templates, aber irgendwie scheine ich das Herleiten von Template-Argumenten noch nicht ganz verstanden zu haben:
Ergibt unter Visual C++:
Code: Alles auswählen
#include <iostream>
template <typename T1, T1 value>
class Class1
{
public:
template <typename T2>
class Class2
{
public:
explicit Class2(
T2 theValue
) : myValue(theValue)
{
return;
}
template <typename T1Other, T1Other valueOther, typename T2Other>
inline Class2(
typename Class1<T1Other, valueOther>::template Class2<T2Other> const & other
) {
myValue = other.myValue;
}
template <typename T1Other, T1Other valueOther, typename T2Other>
inline bool operator==(
typename Class1<T1Other, valueOther>::template Class2<T2Other> const & other
) const {
return (myValue == other.myValue);
}
private:
T2 myValue;
};
Class2<T1> const get() const
{
return Class2<T1>(value);
}
};
int main(int argc, char * argv[])
{
Class1<int, 1> c1;
Class1<int, 2> c2;
std::cout << (c1.get() == c2.get());
}
bzw. live und in Farbecl.exe hat geschrieben:error C2783: 'bool Class1<T1,value>::Class2<T2>::operator ==(const Class1<T1Other,valueOther>::Class2<T2Other> &) const' : could not deduce template argument for 'T1Other'
with
[
T1=int,
value=1,
T2=int
]
see declaration of 'Class1<T1,value>::Class2<T2>::operator =='
with
[
T1=int,
value=1,
T2=int
]
Eigentlich hätte ich gedacht, dass ich den operator== mit hinreichend vielen Template-Parametern ausgestatten hätte, als dass er zumindest einen Treffer finden kann. Liegt es an den verschachtelten Klassen?g++ hat geschrieben:prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:47: error: no match for ‘operator==’ in ‘c1.Class1<T1, value>::get [with T1 = int, T1 value = 1]() == c2.Class1<T1, value>::get [with T1 = int, T1 value = 2]()’