← Back to team overview

dolfin team mailing list archive

Re: Use of explicit

 

On Tue, Jul 08, 2008 at 11:42:25AM +0200, Martin Sandve Alnæs wrote:
> 2008/7/8 Garth N. Wells <gnw20@xxxxxxxxx>:
> >
> >
> > Anders Logg wrote:
> >> On Thu, Jun 26, 2008 at 02:55:30PM +0200, Andy Ray Terrel wrote:
> >>> You probably don't need the explicit keyword for copy constructors as
> >>> done in Functions.h , PetscMatrix.h, uBlasMatrix.h, EpetraMatrix.h.
> >>> This has the added effect of not being able to pass functions as
> >>> arguments (perhaps that is bad but anyhow).  Having just pulled this
> >>> seemed to make my code a bit unhappy.
> >>
> >> You should not need to have a copy constructor to pass a Function as
> >> argument (at least by reference or as a pointer). Or are you passing
> >> it by value? That's probably not a good idea since it would involve
> >> copying the entire mesh as well as the vector of dofs.
> >>
> >
> > This has become an annoyance for me because I do want to pass a Function
> > by value (I really do want a copy) and I can't do
> >
> >   Function f(mesh);
> >   std::vector<Function> F;
> >   F.push_back(f);
> >
> > when explicit is used.
> >
> > Garth
> 
> Then remove the explicit keyword. I may have added it in too many places.
> The point is to avoid strange situations leading to memory leaks and the like.

As far as I understand it, there is never (or very rarely) any point
in making the *copy constructor* explicit.

Making the copy constructor explicit prevents the compiler from
generating temporaries which are needed when calling a function by
value or when returning a new object from a function. If the copy
constructor has been implemented (correctly) then that signifies that
we handle memory issues etc and that temporaries may be safely created.

Maybe our policy should be to make all constructors explicit except
for the copy constructor?

-- 
Anders

Attachment: signature.asc
Description: Digital signature


References