Re: Test on user defined copy-constructor and assignment-operator
Allan W wrote:
Peter K?mmel posted:
Is there a way to ensure that a class has a user defined
copy-constructor and assignment-operator?
Frederick Gotham schrieb:
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 */
}
Peter K?mmel wrote:
I tried it but this code doesn't help, it compiles with msvc.
Huh?
I tried it with .Net 2003, and got the following error message:
error C2512: 'std::basic_ostream<_Elem,_Traits>::__ctor' : no
appropriate default constructor available
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
and
[
_Elem=char,
_Traits=std::char_traits<char>
]
Not the most helpful message, perhaps, but...
if we remove the line that says "Compile ERROR", the error
goes away.
On the contrary, the message shows very clearly that the code is
testing for the presence of a default constructor (which
std::istream doesn't have) as well.
Which version of MSVC were you using? What compile options did
you use? Very strange.
And which iostream? I think that the <iostream.h> in VC++ did
support default construction---and maybe even copy. (On the
other hand, it wasn't in std::.)
--
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! ]