KSharedPtr changes

Frans Englich frans.englich at telia.com
Sun Jan 15 20:32:59 GMT 2006


On Sunday 15 January 2006 17:25, 'Richard Smith' wrote:
> > On Saturday 14 January 2006 08:05, André Wöbbeking wrote:
> >> On Saturday 14 January 2006 13:06, Boudewijn Rempt wrote:
> >> > On Saturday 14 January 2006 12:54, Michel Hermier wrote:
> >> > > One main disaventage of that is that we can't use the
> >> > > "KSharedPtr<Foo> foo = bar;" syntax.
> >> > > We must use the "KSharedPtr<Foo> foo(bar);" syntax.
> >> >
> >> > I'd hate that... We use this construction in so many places in Krita
> >> > that it's going to be decided unfun to convert it everywhere.
> >>
> >> It's worth this little inconvenience. The dangling pointer from Michel's
> >> example is much more anoying.
> >
> > Have you guys actually tested that?  Unless I'm mistaken, in C++, *when
> > constructing an object*, the Foo foo = bar; and Foo foo(bar); syntaxes
> > are identical and equivalent.
>
> Not quite.
>
> "Foo foo = bar;" is called copy-initialization, and it's more-or-less
> equivalent to "Foo foo(static_cast<Foo>(bar));". That is, it constructs a
> Foo temp from the object bar, then calls the Foo copy constructor to
> construct foo (although this copy is allowed to be elided).
>
> "Foo foo(bar);" is called direct-initialization and calls the Foo
> constructor directly.
>
> One difference is that copy-initialization doesn't allow for explicit
> constructors, another is that it may be slower (if the copy constructor
> call is not elided).

Which it in one way or another is[ctor elided], I bet. If not on a higher 
level, I speculate that redudant code elimination would remove the ctor when 
in SSA form after inlining(what case otherwise? Considering how SharedPtr is 
written). However, I don't think it's a good idea to try to figure out what 
the compiler does, but write what is the most convenient, and after that what 
is the most efficient; I use direct-initialization when using smart pointers.


Cheers,

		Frans




More information about the kde-core-devel mailing list