In article <pCAE31HfH8mEFw$>,
Richard Herring <junk@[127.0.0.1]> wrote:
> In message <BUEmg.27154$> , red floyd
> <> writes
> >utab wrote:
> >> Dear all,
> >> How do experienced programmers using this group use the rule of
> >> thumb,namely copy ctor, assignment operator, and dtor.
> >
> >That's the Rule of *THREE*, not the Rule of thumb.
> >
> >> example
> >> if the class does not need them dont use and define them
> >> use them but dont define them (for future revisions of the class as
> >> placeholders)
> >>
> >
> >If I use them, I define them, even if they're trivial. That way I know
> >exactly what's happening, rather than relying on compiler magic.
> >Otherwise, I explicitly hide them.
>
> The compiler is more reliable than I am at keeping track of every single
> data member of a large class. If they are trivial, I already know what
> the "compiler magic" is, so why add clutter to the definition and risk
> getting it wrong?
It can be even worse than that. Defining what would otherwise be
trivial members (copy, assignment or destructor) can lead to a
performance loss. Generic code (such as std::vector) can make
significant optimizations if it knows that its value_type has trivial
special members (which it can detect through the new <type_traits>
library. As soon as you define these special members, they are no
longer trivial (even if they do the same thing as the compiler generated
variant), and generic code must use larger and slower code to ensure
that everything works correctly.
-Howard
|