Topic: Automatic conversion with function templates?
Author: "Igor Solodovnikov" <sivsoft@yahoo.com>
Date: 2000/02/24 Raw View
Your sample doesn't works in visual c++ too:
E:\Program Files\Microsoft Visual Studio\MyProjects\test1\test1.cpp(33) :
error C2784: 'T __cdecl operator *(const class Num<T> &,const class Num<T>
&)' : could not deduce template argument for 'const class Num<T> &' from
'const int'
E:\Program Files\Microsoft Visual Studio\MyProjects\test1\test1.cpp(33) :
error C2677: binary '*' : no global operator defined which takes type 'class
Num<int>' (or there is no acceptable conversion)
I think such behavior is by design. For operator + there is actually only
one implementation in your sample, so compiler only tries to convert
parameters. But for * compiler must generate instance and it can't make
decision about parameters.
Actually why don't you want use friend operators? Anyway here is workaround
for you:
template<class T>
class Num {
public:
Num(T ii) : i(ii) {}
//vc++ needs that const
T get()const { return i; }
friend T operator+(const Num<T>& t1, const Num<T>& t2) {
return T(t1.i + t2.i);
}
private:
T i;
};
template<class T>
T operator*(const Num<T>& t1, const Num<T>& t2) {
return T(t1.get() * t2.get());
}
template<class T>
T operator*(const T&v1, const Num<T>&v2){
return Num<T>(v1)*v2;
}
template<class T>
T operator*(const Num<T>&v1, const T&v2){
return v1*Num<T>(v2);
}
int main() {
Num<int> I = 5;
I = 3 * I; // works
I = I * 3; // works
I = 3 + I; // works
return 0;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: zdv176@zam475.zam.kfa-juelich.de (B.Mohr)
Date: 2000/02/18 Raw View
Hi,
I ran into the following strange thing and hope someone in this group can
explain me what's going wrong:
I currently try to implement some class for numerical computing. Following
the advice in Scott Meyers Effective C++ book, I made the operator+()
and operator*() global to get automatic conversion also on the left
argument. However, after transforming my class into templates (very much
like for the std::complex classes), it doesn't work if I declare the
operator as global function template. Declaring it as inline friend
does work.
1) Why does it work in one case but not the other?
2) If there is a reason, was this intended by the standards committee
or is this an defect?
3) Is there another way to declare my operator's outside the class
template and gettiing automatic conversion on the arguments?
Here is some sample code showing the problem (tested with g++ 2.95.2
and KCC 3.4g):
===========================================================================
template<class T>
class Num {
public:
Num(T ii) : i(ii) {}
T get() { return i; }
friend T operator+(const Num<T>& t1, const Num<T>& t2) {
return T(t1.i + t2.i);
}
private:
T i;
};
template<class T>
T operator*(const Num<T>& t1, const Num<T>& t2) {
return T(t1.get() * t2.get());
}
int main() {
Num<int> I = 5;
I = 3 * I; // ERROR
I = 3 + I; // works
}
===========================================================================
Thanks
Bernd
--
Bernd Mohr / Research Centre Juelich, ZAM, Germany / B.Mohr@fz-juelich.de
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]