C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

implicit constructor declaration (user annoyed)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sb
Guest





PostPosted: Tue Feb 24, 2004 3:29 am    Post subject: implicit constructor declaration (user annoyed) Reply with quote



If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ivan Vecerina
Guest





PostPosted: Tue Feb 24, 2004 1:39 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote



"sb" <spam_bait101 (AT) yahoo (DOT) com> wrote

Hi,
Quote:
If there is at least one user-defined constructor,
no constructors are implicitly declared.

Actually, only the default constructor is not implicitly
declared anymore.
A default copy-constructor is still implicitly defined
(just as the default assignment operator is).

Quote:
struct my_vec : public vector<int> {
NB: Because of the absence of a virtual destructor,

deriving from an STL container tends to be dangerous.
It is not a recommended to do so (a usual alternative
is to use a data member instead, or replace the class
with free functions).
Quote:
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}

'a lot of stupid typing' ? Or just a 1-line declaration to add.

In many designs (such as some polymorphic classes),
it is inappropriate to have a default constructor.
What would you do then?

Quote:
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.

As it is today, many people are having a hard time
*disabling* implicitly generated functions:

class NonCopyableStuff {
...
private: // not implemented and private to disable implicit decls
NonCopyableStuff(NonCopyableStuff const&);
void operator=(NonCopyableStuff const&);
};

Implicitly defined functions are actually quite dangerous.
They might not do the right thing, and are bug-prone.
Consider the very popular "rule of three" of C++:
" Any class requiring a destructor, copy constructor,
or an assignment operator must have all three implemented "
Read for example: http://www.gotw.ca/gotw/069.htm

This rule is so widely agreed on that some would like the
next C++ standard to enforce it by disabling implicit
function generations in some cases...


Kind regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Tue Feb 24, 2004 1:40 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote



Hi,

sb wrote:

Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying?

There are people who find annoying the bare fact that the compiler does
*anything* implicitly.

Quote:
Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.

Why?
If you do not need the default constructor, there is no need "to do a
lot of stupid typing".
If you instead *do* need the default constructor, there is nothing
stupid in writing it, is it?

This rule is in the language because usually, if you write a constructor
(any constructor), it is an indication that the class needs some
specific initialization logic. The compiler humbly steps back here and
says: "OK, you're the boss, if you say that the object needs to be
*specially* constructed, then do it yourself".

Have you also considered:

my_vec(size_t n = 0) : vector<int>(2 * n) {}

It may serve both needs (at least in this particular example).


This is not the main topic here, buit please note also that deriving
publicly from vector<T> is not considered to be a good programming
practice. Most of the time this is a recipe for disaster or at least
indication of fragile design. Other posters will for sure point that out.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andre Heinen
Guest





PostPosted: Tue Feb 24, 2004 1:41 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

On 23 Feb 2004 22:29:37 -0500, [email]spam_bait101 (AT) yahoo (DOT) com[/email] (sb) wrote:

Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

Unless I am mistaken, even if there are user-defined constructors
the compiler still implicitly generates the copy constructor.
Only the generation of the default constructor is disabled.

Quote:
snip
Does anyone else find this behavior annoying?

No, I like it.

Quote:
Instead, I would prefer for the compiler to implicitly declare
all constructors that are not declared by the user.

I would prefer the compiler not to implicitly generate any
constructor at all. When you write your contructors yourself,
you know what they do. When they are silently and implicitly
generated, you always have to be on your guard and ensure they
really do what you want.

Actually, occasionally I go so far as to disable implicit
generation by declaring the offending contructors (and the
assignment operator) private, and by giving them no definition.

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Risto Lankinen
Guest





PostPosted: Tue Feb 24, 2004 1:42 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

Hi!

"sb" <spam_bait101 (AT) yahoo (DOT) com> wrote

Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.

But some classes might absolutely require some construction
argument. Letting default constructor be always synthesized
would make it impossible for the programmer to enforce the
requirement of a construction argument.

But what you are asking makes sense in some cases. Since
there already is a way to disable accidental auto-generation
(by declaring said auto-generated functions in the "private"
section, and dever defining them to prevent accidental use from
the class itself), a corresponding mechanism to *enable* the
auto-generation in the presence of additional ctors could be
useful indeed.

What would be a good syntax for such feature? I'd suggest
the following:

class C
{
public:
C( SomeType arg );

using C(); // proposed syntax
using C( const C & ); // proposed syntax
};

Cheers!

- Risto -


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeff Schwab
Guest





PostPosted: Tue Feb 24, 2004 7:14 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

sb wrote:
Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.

"All" constructors? How would the compiler know which constructors are
needed? Default constructors would not be meaningful for some types.

Anyway, your example is tedious. You don't need to indicate the base
class's default constructor explicitly (that's why it's called a
default), and you could have used a default argument to your first
constructor.

struct my_vec: vector<int> {
double foo;
my_vec(size_t n =0) : vector<int>(2*n) {}
};


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Craig Henderson
Guest





PostPosted: Tue Feb 24, 2004 7:19 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

You're typing more than you need

my_vec() { }

is sufficient, you don't need to explicitly call the default ctor of the
base class.

Alternatively, you could give the ctor parameter a default value, so it can
be used either way:
my_vec(size_t n=0) : vector<int>(2*n) { }

FWIW, I don't find it annoying, as I like to be explicit about what's
defined for a class, so I generally define all compiler generated functions
myself anyway, even if I don't implement them. Maybe that's just me :-)

-- Craig


"sb" <spam_bait101 (AT) yahoo (DOT) com> wrote

Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andre Heinen
Guest





PostPosted: Tue Feb 24, 2004 7:21 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

On 24 Feb 2004 08:40:51 -0500, Maciej Sobczak
<no.spam (AT) no (DOT) spam.com> wrote:
Quote:

This is not the main topic here, buit please note also that deriving
publicly from vector<T> is not considered to be a good programming
practice. Most of the time this is a recipe for disaster or at least
indication of fragile design. Other posters will for sure point that out.

Well, Ivan did, and told us why: because of the absence of a
virtual destructor. I must say it surprises me. Of course, I
know that no virtual destructor means no polymorphism. But many
classes are designed that way. How can deriving from std::vector
be more dangerous than deriving from any other non-virtual class?

As an example, in the standard library itself, aren't file stream
classes derived from one another without any virtual destructor?

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Feb 24, 2004 7:26 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

[email]spam_bait101 (AT) yahoo (DOT) com[/email] (sb) wrote in message
news:<221dd125.0402231407.7275ea1d (AT) posting (DOT) google.com>...

Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying?

No.

Quote:
Instead, I would prefer for the compiler to implicitly declare all
constructors that are not declared by the user.

Actually, I'd prefer for the compiler not to declare anything
implicitly. But that would create problems of C compatibility; the
current rule seems fairly reasonable. (Even better might be for the
presense of any user defined constructor, destructor or assignment
operator to suppress all of the compiler generated defaults -- including
copy construction and assignment.)

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steve Dewhurst
Guest





PostPosted: Tue Feb 24, 2004 7:27 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

[email]spam_bait101 (AT) yahoo (DOT) com[/email] (sb) wrote in message news:<221dd125.0402231407.7275ea1d (AT) posting (DOT) google.com>...
Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.
....
Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.

Not me. I like to have the option of forcing users of my classes to
supply an initializer explicitly.

Steve
Steve Dewhurst
www.semantics.org

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Tue Feb 24, 2004 7:28 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

In message <T1E_b.9936$k4.214112 (AT) news1 (DOT) nokia.com>, Risto Lankinen
<rlankine (AT) hotmail (DOT) com> writes
Quote:
What would be a good syntax for such feature? I'd suggest
the following:

class C
{
public:
C( SomeType arg );

using C(); // proposed syntax
using C( const C & ); // proposed syntax
};

Cheers!
Well my proposal goes somewhat further and will be discussed at the next

WG21 meeting. Here is a relevant extract from my paper:

2 The Proposals
1) Provide a specific mechanism to turn off all implicit member
function generation. For this I propose that we enhance the grammar of
class definition to allow a class to be qualified as explicit. In such a
class none of the four special functions will be implicitly declared.
2) Provide a mechanism to explicitly declare a default constructor,
copy constructor, copy assignment and destructor. For this purpose I
propose that 'default' preceding the declaration explicitly requires the
member function be compiler generated. The generation of an explicitly
declared default constructor is not suppressed by the declaration of
other constructors. The rules for compiler generation of the four
members are as they are for the generation of the implicit versions.
[12.1 para. 7 for implicit default constructor, 12.8 para. 4-8 for the
copy constructor, 12.8 para. 10-14 for copy assignment, 12.4 para. 3-5,]
Example:
explicit class example{
public:
default example();
example(int * i_ptr):val(*i_ptr){}
default virtual ~example();
private:
int val;
};
// note the class is silly as such but is sufficient to demonstrate the
combined
// use of explicit and default.

class derived: public example{
public:
// public interface members
private:
std::string s;
};
int main(){
int i(12);
example e1; // OK, uses compiler generated default
example e2(&i); // OK uses second constructor
example e3(e1); // ERROR no implicit copy ctor
derived d1; // OK
derived d2(d1); //ERROR, cannot generate copy ctor
example* d_ptr = new derived;
delete d_ptr; // calls an implicitly generated
// ~derived() first which calls ~string
}

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Wed Feb 25, 2004 2:53 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

In message <221dd125.0402231407.7275ea1d (AT) posting (DOT) google.com>, sb
<spam_bait101 (AT) yahoo (DOT) com> writes
Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.

Well learn to live with the current behaviour because any changes will
not be in the direction that will reduce your annoyance. You preference
would break several well established and commonly used C++ idioms.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Nicola Musatti
Guest





PostPosted: Wed Feb 25, 2004 2:54 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

Andre Heinen <nospam (AT) nospam (DOT) invalid> wrote

[...]
Quote:
Well, Ivan did, and told us why: because of the absence of a
virtual destructor. I must say it surprises me. Of course, I
know that no virtual destructor means no polymorphism. But many
classes are designed that way. How can deriving from std::vector
be more dangerous than deriving from any other non-virtual class?

It isn't.

Quote:
As an example, in the standard library itself, aren't file stream
classes derived from one another without any virtual destructor?

No they aren't. The root of the hyerarchy is std::ios_base which has a
virtual destructor.

Cheers,
Nicola Musatti

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Feb 25, 2004 3:00 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

sb wrote:
Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying?

Yes.

Quote:
Instead, I would prefer for the compiler to implicitly declare all
constructors that are not declared by the user.

That would add to the existing problem of having to disable
implicitly-declared functions in classes that aren't value classes.

There is a proposal to modify the C++ syntax to allow the programmer
to explicitly enable or disable the special member functions that are
now implicitly declared:
<http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1445.htm>.

{However note that this is superseded by n1582 in the pre-Sydney
mailing. -mod/fwg}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Drew Eckhardt
Guest





PostPosted: Wed Feb 25, 2004 3:01 pm    Post subject: Re: implicit constructor declaration (user annoyed) Reply with quote

In article <221dd125.0402231407.7275ea1d (AT) posting (DOT) google.com>,
sb <spam_bait101 (AT) yahoo (DOT) com> wrote:
Quote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

Actually if the user has not declared a copy constructor one
will be declared as public and implemented using the members' copy
constructors. This can have interesting effects.

Quote:
Does anyone else find this behavior annoying?

It's good. If there aren't reasonable defaults I want a compile-time
error without having to trick the compiler
class foo {
private:
// Never instantiate. Code (I don't know how all my
// template libraries are implemented - they're black boxes) is likely
// to fail to compile because of the access, and if it does the link
// fail although that might not happen until run-time at a
// customer's site and I get an angry call at some oscene
// time...
foo(const foo&);
};
to supress the behavior.

Personally, I'd have preferred the implicit default constructor, copy
constructor, and assignment operator generation to be limited to
structs with no member functions (for backwards compatability with 'C').
--
<a href="http://www.poohsticks.org/drew/">Home Page</a>
Life is a terminal sexually transmitted disease.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.