Topic: Implicit constructors


Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Fri, 6 Sep 2002 15:35:07 +0000 (UTC)
Raw View
In article <d6651fb6.0209040705.7ee434f@posting.google.com>, James Kanze
<kanze@gabi-soft.de> writes
>The compiler generated versions are the same as if the implementation
>was within the class definition, I think.  Thus, they are declared
>"inline".  Obviously, the compiler is not required to actually generate
>anything inline.  The "inline" in this case means "declared inline"

Exactly, and so can breech ODR.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Tue, 3 Sep 2002 16:44:13 +0000 (UTC)
Raw View
In article <3d6f28a9.1227812@news.earthlink.net>, John Potter
<jpotter@falcon.lhup.edu> writes
>> Whether it
>> declares it or not is completely irrelevant (well can you provide a
>> place where declaration without definition would be possible?)
>
>We do it all the time for copy ctor and operator= to make a class
>non-copy.  If we can do it, why not the implementation?

I was unclear in my wording. I should have written '... where implicit
declaration...' Please demonstrate an example of an implicit declaration
that can be detected even though it is not 'called'.

An untestable requirement in the Standard is vacuous. I contend (until
someone produces a counter example) that the statements about
declaration of copy ctor, copy assignment and dtor are unenforceable.

BTW, the case of the copy ctor is like that of a default ctor in that
the compiler notionally declares a copy ctor with a const ref parameter
unless the programmer declares something else that whose first parameter
is a possibly cv qualified reference and all subsequent parameter have
default arguments.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Tue, 3 Sep 2002 16:50:51 +0000 (UTC)
Raw View
In article <3d6ff064.9008125@news.earthlink.net>, John Potter
<jpotter@falcon.lhup.edu> writes
>On 30 Aug 2002 19:55:01 GMT, Francis Glassborow
><francis.glassborow@ntlworld.com> wrote:
>
>> Yes, it is untestable and implementors are free to ignore the
>> requirement. They do not even have to worry about a unique point of
>> definition because breeches of the ODR do not require a diagnostic. It
>> is actually hard to breech the ODR for compiler generated definitions of
>> copy ctors and default ctors but not completely impossible.
>
>It is impossible.  The compiler generated members are inline.  The ODR
>has nothing to do with it.

Are you saying that a compiler MUST actually inline a generated
ctor/dtor? First note that it cannot do that for a virtual dtor; it
needs an address for the vtable (or whatever is used instead). Second,
for classes with large sub-object lattices that would require code bloat
(and if a member were an array, it might exceed the complexity that the
compiler supports for inlining).

Now how do we break ODR? Well there is a nasty little wart that allows
different default arguments in different translation units. That means
if a default ctor is provided for a sub-object via a default argument
for the first parameter of a ctor for that sub-object, the compiler
generated ctor  for the whole object will be different in the different
TU's. And that difference becomes visible if the class in question is a
template type parameter. That is surely a breech of the ODR.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kanze@gabi-soft.de (James Kanze)
Date: Wed, 4 Sep 2002 19:31:05 +0000 (UTC)
Raw View
francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message
news:<gd3tjbBO+Jc9Ew4V@robinton.demon.co.uk>...
> In article <3d6ff064.9008125@news.earthlink.net>, John Potter
> <jpotter@falcon.lhup.edu> writes
> >On 30 Aug 2002 19:55:01 GMT, Francis Glassborow
> ><francis.glassborow@ntlworld.com> wrote:

> >> Yes, it is untestable and implementors are free to ignore the
> >> requirement. They do not even have to worry about a unique point of
> >> definition because breeches of the ODR do not require a
> >> diagnostic. It is actually hard to breech the ODR for compiler
> >> generated definitions of copy ctors and default ctors but not
> >> completely impossible.

> >It is impossible.  The compiler generated members are inline.  The
> >ODR has nothing to do with it.

> Are you saying that a compiler MUST actually inline a generated
> ctor/dtor?

The compiler generated versions are the same as if the implementation
was within the class definition, I think.  Thus, they are declared
"inline".  Obviously, the compiler is not required to actually generate
anything inline.  The "inline" in this case means "declared inline".

--
James Kanze                           mailto:jkanze@caicheuvreux.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Andrey Tarasevich <andreytarasevich@hotmail.com>
Date: 30 Aug 2002 18:40:05 GMT
Raw View
Francis Glassborow wrote:
> ...
> >Check the standard.  The implementation will unconditionally declare
> >the ctor.  You are forbidden to define it.  The implementation will
> >define it only if it is used.  The program is ill-formed if the
> >defined ctor is invalid.
>
> OK but those are really word games (in the standard).

Word games or not, the standard is very clear and specific here.

> Whether it
> declares it or not is completely irrelevant (well can you provide a
> place where declaration without definition would be possible?)

It is always possible. Everything that can be declared and defined in
C++ may be left without definition provided it is not used in the
program or is used in certain limited number of ways.

> I do not
> believe that it is possible to determine that the implementation has
> declared a default dtor for which it has not generated a definition.

I believe it is possible. Let's take a look at this simple code

struct S { int& r; };
int main { sizeof(S().r); }

This program is well-formed (also see below). In this case the compiler
must declare the constructor, but must not define it.

(Actually, the wording in 3.2/2 doesn't seem to be very clear to me and
it's hard to say whether the above code is actually "using" the
constructor. I believe it is not, because the expression 'S().r' as
operand of 'sizeof' is not potentially evaluated. Comeau compiler
disagrees with me on this. It makes an attempt to define the
constructor, which results in diagnostic)

Best regards,
Andrey Tarasevich,
Brainbench C and C++ Programming MVP

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Fri, 30 Aug 2002 14:53:52 CST
Raw View
In article <2869c790.0208300338.4b7d3921@posting.google.com>, Dave Moore
<dtmoore@email.unc.edu> writes
>> OK but those are really word games (in the standard). Whether it
>> declares it or not is completely irrelevant (well can you provide a
>> place where declaration without definition would be possible?)
>
>class A {
>  int a;
>public:
>  // interface specification w/o ctors
>};
>
>   Assume you have compilation unit including the ctor-less class
>declaration above(from a header file), but that no objects of that
>type are *created* anywhere in the compilation unit (as far as I can
>see they may still be used in other ways).  In that case, there
>_should_ be a declaration of the ctor for class A, but no object code
>will be created for the default ctor (hence it is not defined).

And hence the existence of the declaration is untestable. These are arid
word games. They add nothing to the requirements of a real
implementation.

>
>> I do not believe that it is possible to determine that the implementation
>> has declared a default dtor for which it has not generated a definition.
>> Actually, assuming that I am right, and it is untestable, I think the
>> unconditional declaration requirement is a defect because no test suite
>> could test conformance.
>>
>
>   I am by no means an expert in this area, but it seems to me that a
>test suite could test for the declaration using the symbol table in
>the .o file.  I am not sure how exactly, I just know that I get
>link-time errors about undefined symbols when I try to call a function
>that has been declared but not defined ... this implies that the
>symbol name must exist somewhere, it just doesn't point to anything.
>   Uh oh, it occurs to me now that anything involving the symbol table
>is probably implementation defined, so maybe general inclusion in a
>test-suite is not possible?  Like I said, I am not an expert ... and
>maybe I am in over my head here 8*).

Yes, it is untestable and implementors are free to ignore the
requirement. They do not even have to worry about a unique point of
definition because breeches of the ODR do not require a diagnostic. It
is actually hard to breech the ODR for compiler generated definitions of
copy ctors and default ctors but not completely impossible.



--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Fri, 30 Aug 2002 21:30:07 CST
Raw View
On 30 Aug 2002 19:55:01 GMT, Francis Glassborow
<francis.glassborow@ntlworld.com> wrote:

> Yes, it is untestable and implementors are free to ignore the
> requirement. They do not even have to worry about a unique point of
> definition because breeches of the ODR do not require a diagnostic. It
> is actually hard to breech the ODR for compiler generated definitions of
> copy ctors and default ctors but not completely impossible.

It is impossible.  The compiler generated members are inline.  The ODR
has nothing to do with it.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Allan_W@my-dejanews.com (Allan W)
Date: Fri, 30 Aug 2002 23:04:44 CST
Raw View
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote
> > > If user declares no default constructor
> > > for a class, the compiler will always declare it implicitly.

> Allan W wrote:
> > I think you mean that the compiler will do so if there is no user-defined
> > constructor at all.
>
> Yes.
>
> > But what about:
> >
> >     class foo {
> >         int &intref;
> >     };
> >
> > Will the compiler create a default constructor? What will it use to
> > initialize intref?
> > ...
>
> Create? No, it won't create it, if by saying "create" you mean "define".
> Compiler won't _define_ the default constructor for such class 'foo'.
> However, it will still _declare_ it. The OP's question was about
> declaration. The definition is a separate issue.

Semantics?

When you declare a constructor without declaring it, you guarantee that
nobody can construct it. The compiler does not do this.

12.1/7: An implicitly-declared default constructor for a class is
        *implicitly defined* when it is used to create an object
        of its class type (1.8). The implicitly-defined default
        constructor performs the set of initializations of the
        class that would be performed by a user-written default
        constructor for that class with an empty
        *mem-initializer-list* (12.6.2) and an empty function body.
        If that user-written default constructor would be ill-formed,
        the program is ill-formed.

So, if you create a foo object, then the compiler automatically defines
a constructor like this:

     class foo {
         int &intref;
     public: foo() {}
     };

which is ill-formed because intref isn't initialized.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: 31 Aug 2002 04:15:15 GMT
Raw View
Francis Glassborow <francis.glassborow@ntlworld.com> writes:

> In article <3d6e730e.31126000@news.earthlink.net>, John Potter
> <jpotter@falcon.lhup.edu> writes
> >Check the standard.  The implementation will unconditionally declare
> >the ctor.  You are forbidden to define it.  The implementation will
> >define it only if it is used.  The program is ill-formed if the
> >defined ctor is invalid.

> OK but those are really word games (in the standard). Whether it declares it
> or not is completely irrelevant (well can you provide a place where
> declaration without definition would be possible?) I do not believe that it
> is possible to determine that the implementation has declared a default dtor
> for which it has not generated a definition. Actually, assuming that I am
> right, and it is untestable, I think the unconditional declaration
> requirement is a defect because no test suite could test conformance.

struct Y
{
    Y(int); // no default constructor
};

struct X
{
    Y y;
}; // implicit default constructor declared
// but cannot be defined

const unsigned a=sizeof(new X()); // OK, default constructor declared
const unsigned b=sizeof(new Y()); // error, no default constructor
X x; // error, default constructor cannot be defined

Anthony

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: alexeivm@rusf.ru (Alexei Monastyrsky)
Date: 27 Aug 2002 17:20:05 GMT
Raw View
Hi,
   Under what circumstances, according to the Standard, a default
constructor is not declared implicitly despite the absence of a
user-declared default constructor?

--
Alexei

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Hyman Rosen <hyrosen@mail.com>
Date: Tue, 27 Aug 2002 13:08:23 CST
Raw View
Alexei Monastyrsky wrote:
>    Under what circumstances, according to the Standard, a default
> constructor is not declared implicitly despite the absence of a
> user-declared default constructor?

12.1/5. An implict default constructor is declared only if there
is no user-declared constructor at all. If the user declares any
constructor, the compiler will not generate an implicit default
constructor. Whether the user-declared constructor is a default
constructor is immaterial.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Andrey Tarasevich <andreytarasevich@hotmail.com>
Date: 27 Aug 2002 21:15:04 GMT
Raw View
Alexei Monastyrsky wrote:
> ...
>    Under what circumstances, according to the Standard, a default
> constructor is not declared implicitly despite the absence of a
> user-declared default constructor?
> ...

There are no such circumstances. If user declares no default constructor
for a class, the compiler will always declare it implicitly.

Best regards,
Andrey Tarasevich,
Brainbench C and C++ Programming MVP

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Victor Bazarov" <vAbazarov@dAnai.com>
Date: 28 Aug 2002 17:25:01 GMT
Raw View
"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:3D6BDF60.8E608D9C@hotmail.com...
> Alexei Monastyrsky wrote:
> > ...
> >    Under what circumstances, according to the Standard, a default
> > constructor is not declared implicitly despite the absence of a
> > user-declared default constructor?
> > ...
>
> There are no such circumstances. If user declares no default constructor
> for a class, the compiler will always declare it implicitly.

Wait, isn't the default constructor absent if there any other
constructor declared?  If we write

    struct A
    {
        A(int);
    };

, 'A' will have _no_ default constructor (implicit will not be
generated) by the compiler.

Victor
--
Please remove capital A's from my address when replying by mail


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Andrey Tarasevich <andreytarasevich@hotmail.com>
Date: Wed, 28 Aug 2002 12:46:16 CST
Raw View
Andrey Tarasevich wrote:
> ...
> > ...
> >    Under what circumstances, according to the Standard, a default
> > constructor is not declared implicitly despite the absence of a
> > user-declared default constructor?
> > ...
>
> There are no such circumstances. If user declares no default constructor
> for a class, the compiler will always declare it implicitly.
> ...

My answer is incorrect (thanks to Ron Natalie for pointing it out).
Presence of _any_ user-declared constructor in the class will prevent
the compiler from declaring the default constructor.

So the correct is yes, there are such circumstances. For example:

class C
{
  C(int);
};

There is no user-declared default constructor in this class. And the
compiler will not declare one either.

Best regards,
Andrey Tarasevich,
Brainbench C and C++ Programming MVP

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 28 Aug 2002 17:50:05 GMT
Raw View
In article <3D6BDF60.8E608D9C@hotmail.com>, Andrey Tarasevich
<andreytarasevich@hotmail.com> writes
>Alexei Monastyrsky wrote:
>> ...
>>    Under what circumstances, according to the Standard, a default
>> constructor is not declared implicitly despite the absence of a
>> user-declared default constructor?
>> ...
>
>There are no such circumstances. If user declares no default constructor
>for a class, the compiler will always declare it implicitly.

Think again. I think that you will find most classes are examples of why
your statement is false, i.e. they do not have a default ctor and yet
the compiler is not allowed to attempt to generate one because the suer
has declared some (non-default) ctor.


>
>Best regards,
>Andrey Tarasevich,
>Brainbench C and C++ Programming MVP

What does MVP stand for?

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 28 Aug 2002 12:46:23 CST
Raw View
In article <1030469893.91508@master.nyc.kbcfp.com>, Hyman Rosen
<hyrosen@mail.com> writes
>Alexei Monastyrsky wrote:
>>    Under what circumstances, according to the Standard, a default
>> constructor is not declared implicitly despite the absence of a
>> user-declared default constructor?
>
>12.1/5. An implict default constructor is declared only if there
>is no user-declared constructor at all. If the user declares any
>constructor, the compiler will not generate an implicit default
>constructor. Whether the user-declared constructor is a default
>constructor is immaterial.

And the trap here is that even a user declared copy ctor disables the
compiler's ability to generate a default ctor. However not the other way
round, only a user declared ctor that can operate as a copy ctor (i.e.
its first parameter is a possibly cv qualified reference to the class
type and all other parameters have default arguments) disables the
compilers from generating a default copy ctor (one whose only parameter
is a const ref to the class type)



--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Hyman Rosen <hyrosen@mail.com>
Date: 28 Aug 2002 17:50:21 GMT
Raw View
Andrey Tarasevich wrote:
 > If user declares no default constructor for a class,
 > the compiler will always declare it implicitly.

No, this is incorrect. If the user declares no
(not "no default", but none at all) constructor,
the compiler declares a default constructor. If
the user declares any constructor at all, the
compiler does not declare a default constructor.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Adam H. Peterson" <ahp6@email.byu.edu>
Date: Wed, 28 Aug 2002 14:43:07 CST
Raw View
> >    Under what circumstances, according to the Standard, a default
> > constructor is not declared implicitly despite the absence of a
> > user-declared default constructor?
>
> 12.1/5. An implict default constructor is declared only if there
> is no user-declared constructor at all. If the user declares any
> constructor, the compiler will not generate an implicit default
> constructor. Whether the user-declared constructor is a default
> constructor is immaterial.

Correct me if I'm wrong, but I believe that a default constructor will also
not be implicitly generated if any base class or member does not have a
default constructor.  Is it not so?


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Allan_W@my-dejanews.com (Allan W)
Date: 28 Aug 2002 22:20:04 GMT
Raw View
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote
> If user declares no default constructor
> for a class, the compiler will always declare it implicitly.

I think you mean that the compiler will do so if there is no user-defined
constructor at all.

But what about:

    class foo {
        int &intref;
    };

Will the compiler create a default constructor? What will it use to
initialize intref?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Andrey Tarasevich <andreytarasevich@hotmail.com>
Date: 28 Aug 2002 23:40:04 GMT
Raw View
Allan W wrote:
> ..
> > If user declares no default constructor
> > for a class, the compiler will always declare it implicitly.
>
> I think you mean that the compiler will do so if there is no user-defined
> constructor at all.

Yes.

> But what about:
>
>     class foo {
>         int &intref;
>     };
>
> Will the compiler create a default constructor? What will it use to
> initialize intref?
> ...

Create? No, it won't create it, if by saying "create" you mean "define".
Compiler won't _define_ the default constructor for such class 'foo'.
However, it will still _declare_ it. The OP's question was about
declaration. The definition is a separate issue.

Best regards,
Andrey Tarasevich,
Brainbench C and C++ Programming MVP

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 29 Aug 2002 17:00:17 GMT
Raw View
In article <3D6D5AAB.D75C082B@hotmail.com>, Andrey Tarasevich
<andreytarasevich@hotmail.com> writes
>Allan W wrote:
>> ..
>> > If user declares no default constructor
>> > for a class, the compiler will always declare it implicitly.
>>
>> I think you mean that the compiler will do so if there is no user-defined
>> constructor at all.
>
>Yes.
>
>> But what about:
>>
>>     class foo {
>>         int &intref;
>>     };
>>
>> Will the compiler create a default constructor? What will it use to
>> initialize intref?
>> ...
>
>Create? No, it won't create it, if by saying "create" you mean "define".
>Compiler won't _define_ the default constructor for such class 'foo'.
>However, it will still _declare_ it. The OP's question was about
>declaration. The definition is a separate issue.

No. Were the compiler to automatically declare it I could provide a
definition somewhere else of the form:

foo::foo(): intref(i) {}

assuming that there is an i in scope.

But you cannot do that. The compiler generates definitions if it can, if
it cannot it should issue a diagnostic.

>
>Best regards,
>Andrey Tarasevich,
>Brainbench C and C++ Programming MVP

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Thu, 29 Aug 2002 14:39:47 CST
Raw View
On 29 Aug 2002 17:00:17 GMT, Francis Glassborow
<francis.glassborow@ntlworld.com> wrote:

> In article <3D6D5AAB.D75C082B@hotmail.com>, Andrey Tarasevich
> <andreytarasevich@hotmail.com> writes

> > Allan W wrote:

> >> But what about:

> >>     class foo {
> >>         int &intref;
> >>     };

> >> Will the compiler create a default constructor? What will it use to
> >> initialize intref?
> >> ...

> >Create? No, it won't create it, if by saying "create" you mean "define".
> >Compiler won't _define_ the default constructor for such class 'foo'.
> >However, it will still _declare_ it. The OP's question was about
> >declaration. The definition is a separate issue.

> No. Were the compiler to automatically declare it I could provide a
> definition somewhere else of the form:

Check the standard.  The implementation will unconditionally declare
the ctor.  You are forbidden to define it.  The implementation will
define it only if it is used.  The program is ill-formed if the
defined ctor is invalid.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 30 Aug 2002 06:55:01 GMT
Raw View
In article <3d6e730e.31126000@news.earthlink.net>, John Potter
<jpotter@falcon.lhup.edu> writes
>> >Create? No, it won't create it, if by saying "create" you mean "define".
>> >Compiler won't _define_ the default constructor for such class 'foo'.
>> >However, it will still _declare_ it. The OP's question was about
>> >declaration. The definition is a separate issue.
>
>> No. Were the compiler to automatically declare it I could provide a
>> definition somewhere else of the form:
>
>Check the standard.  The implementation will unconditionally declare
>the ctor.  You are forbidden to define it.  The implementation will
>define it only if it is used.  The program is ill-formed if the
>defined ctor is invalid.

OK but those are really word games (in the standard). Whether it
declares it or not is completely irrelevant (well can you provide a
place where declaration without definition would be possible?) I do not
believe that it is possible to determine that the implementation has
declared a default dtor for which it has not generated a definition.
Actually, assuming that I am right, and it is untestable, I think the
unconditional declaration requirement is a defect because no test suite
could test conformance.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: 30 Aug 2002 15:55:08 GMT
Raw View
On 30 Aug 2002 06:55:01 GMT, Francis Glassborow
<francis.glassborow@ntlworld.com> wrote:

> In article <3d6e730e.31126000@news.earthlink.net>, John Potter
> <jpotter@falcon.lhup.edu> writes

> >> >Create? No, it won't create it, if by saying "create" you mean "define".
> >> >Compiler won't _define_ the default constructor for such class 'foo'.
> >> >However, it will still _declare_ it. The OP's question was about
> >> >declaration. The definition is a separate issue.

> >> No. Were the compiler to automatically declare it I could provide a
> >> definition somewhere else of the form:

> >Check the standard.  The implementation will unconditionally declare
> >the ctor.  You are forbidden to define it.  The implementation will
> >define it only if it is used.  The program is ill-formed if the
> >defined ctor is invalid.

> OK but those are really word games (in the standard).

Word games required for members.  They must be declared within the
class definition.  At the end of the definition, the big three will be
declared either by the user or the implementation.  In the absence of
other ctors, this also applies to the default ctor.

> Whether it
> declares it or not is completely irrelevant (well can you provide a
> place where declaration without definition would be possible?)

We do it all the time for copy ctor and operator= to make a class
non-copy.  If we can do it, why not the implementation?

> I do not
> believe that it is possible to determine that the implementation has
> declared a default dtor for which it has not generated a definition.
> Actually, assuming that I am right, and it is untestable, I think the
> unconditional declaration requirement is a defect because no test suite
> could test conformance.

Members are the defect.  Let's get rid of them. :)  Note that the same
problem does not exist for unary operator& and operator, which will be
defined by the implementation if used.  The definition can be the
declaration and no declaration is needed unless used.  I guess it goes
without stating that the implementation must make them non-members
even though the user has the option to make them members.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dtmoore@email.unc.edu (Dave Moore)
Date: 30 Aug 2002 15:55:15 GMT
Raw View
[SNIP]
> OK but those are really word games (in the standard). Whether it
> declares it or not is completely irrelevant (well can you provide a
> place where declaration without definition would be possible?)

class A {
  int a;
public:
  // interface specification w/o ctors
};

   Assume you have compilation unit including the ctor-less class
declaration above(from a header file), but that no objects of that
type are *created* anywhere in the compilation unit (as far as I can
see they may still be used in other ways).  In that case, there
_should_ be a declaration of the ctor for class A, but no object code
will be created for the default ctor (hence it is not defined).

> I do not believe that it is possible to determine that the implementation
> has declared a default dtor for which it has not generated a definition.
> Actually, assuming that I am right, and it is untestable, I think the
> unconditional declaration requirement is a defect because no test suite
> could test conformance.
>

   I am by no means an expert in this area, but it seems to me that a
test suite could test for the declaration using the symbol table in
the .o file.  I am not sure how exactly, I just know that I get
link-time errors about undefined symbols when I try to call a function
that has been declared but not defined ... this implies that the
symbol name must exist somewhere, it just doesn't point to anything.
   Uh oh, it occurs to me now that anything involving the symbol table
is probably implementation defined, so maybe general inclusion in a
test-suite is not possible?  Like I said, I am not an expert ... and
maybe I am in over my head here 8*).

Dave Moore

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]