On Apr 16, 7:11 am, Francis Glassborow
<francis.glassbo...@btinternet.com> wrote:
Let me give you a simple example of why I think default
initialisation is a very bad idea (certainly without a diagnostic
being issued, and if the compiler is going to issue a warning why
not just stop there)
void show_value(int * const ptr){std::cout << *ptr;}
// note the assumption that code makes
int main(){
int * i_ptr;
foo(i_ptr);
}
Now a halfway respectable compiler at a high enough warning level
will issue me a warning for that code. However when I explicitly or
implicitly (through a compiler default) initialise i_ptr to 0 the
compiler can only detect my error by a full program analysis. If
you think the compiler should be able to diagnose with less than
that, think what would happen if foo() was in a separate
translation unit.
Alright, then consider this slightly modified version:
void show_value(std::shared_ptr<int const> ptr){std::cout << *ptr;}
// note the assumption that code makes
int main() {
std::shared_ptr<int> i_ptr;
foo(i_ptr);
}
This is something that can be written in C++0x, or, for that matter,
in C++03 with TR1 today, with i_ptr being implicitly initialized to
a null pointer. For any arguments along the lines of "I do not want
the compiler to correct my code silently", they would also have to
explain why a smart pointer in this case should be treated any
differently from a raw pointer.
of the shared_ptr class. If we don't want that, we can avoid writing
such a constructor.
supply the default constructor. That way it is impossible to forget to
initialize the objects of that type.
[ comp.lang.c++.moderated. First time posters: Do this! ]