Re: Test on user defined copy-constructor and assignment-operator
Frederick Gotham wrote:
Peter K?mmel posted:
Is there a way to ensure that a class has a user defined
copy-constructor and assignment-operator?
Maybe something along the following lines:
template<class T>
class Var {
typedef int EnsureCopyCstr[ !!sizeof T(T()) ];
public:
Var()
{
}
};
#include <ostream>
#include <vector>
#include <string>
int main()
{
Var<int> obj1;
Var<std::vector<double> > obj2;
Var<std::string> obj3;
Var<std::ostream> obj4; /* Compile ERROR */
}
Two problems, I suspect. The first is that it will also fail if
the class doesn't have an accessible default constructor; this
is the case for std::ostream, for example. The second is that
it tests whether the type is copy constructable, not whether it
has a user defined copy-constructor. If Peter means what he
says, then what he needs should also fail for Var<int>.
(I don't think what he says he wants is possible, because a
compiler generated copy-constructor should be indistinguishable
from a user written one. It should be possible to distinguish
whether the constructor is trivial or not, however, and that's
probably a more useful information anyway.)
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]