Topic: Is this a BC5.0 bug or is it valid C++ ?


Author: Morten Christensen <mor@dit.ou.dk>
Date: 1996/06/17
Raw View
#include <iostream.h>
#include <typeinfo.h>

struct A
{
};

int main()
{
 A a;
 A b();

 cout << "Type of a : " << typeid(a).name() << "\n";
 cout << "Type of b : " << typeid(b).name() << "\n";

 return 0;
}

- Output (Borland C++ 5.0) :

Type of a : A
Type of b : A (*)()

....

As this example shows, b is not an object but a pointer to a function
returning an A..... Not what I expected!

Can anyone tell if this is how correct, standard C++ works or if it is
due to a bug in BC5.0 ?

Thanks,
Morten M. Christensen
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: tony@online.tmx.com.au (Tony Cook)
Date: 1996/06/17
Raw View
Morten Christensen (mor@dit.ou.dk) wrote:
: #include <iostream.h>
: #include <typeinfo.h>

: struct A
: {
: };

: int main()
: {
:  A a;
:  A b();

This declares a function.  b is a function taking no parameters,
returning A.

:  cout << "Type of a : " << typeid(a).name() << "\n";
:  cout << "Type of b : " << typeid(b).name() << "\n";

b is converted to a pointer to a function taking no parameters and
returning A.

:  return 0;
: }

: - Output (Borland C++ 5.0) :

: Type of a : A
: Type of b : A (*)()

This is correct.

: ....

: As this example shows, b is not an object but a pointer to a function
: returning an A..... Not what I expected!

: Can anyone tell if this is how correct, standard C++ works or if it is
: due to a bug in BC5.0 ?

It is draft standard C++.  Except for typeid() it's even valid ARM
C++.

--
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: claus@faerber.muc.de (Claus A. Faerber)
Date: 1996/06/19
Raw View
## Morten Christensen <mor@dit.ou.dk> wrote on 17 Jun 96: ##

> int main()
> {
>  A a;
>  A b();

Should the compiler treat this as a function prototype or as
the creation of an object?!

> - Output (Borland C++ 5.0) :
>
> Type of a : A
> Type of b : A (*)()
>
> As this example shows, b is not an object but a pointer to a
> function returning an A..... Not what I expected!

This problem occurs with all ctors due to their syntax; the
only solution is to have a look at the parameter list, if
they are valid objects/variables/consts, it is a ctor call,
if it consists of types, it is a function definition:

        A c(1);         // ctor A::A(int) called
        A d(int);       // function A(d)(int)

But that can not work with no parameters, so the only
solution then is to add the brackets for a function
prototype and to omit them for a ctor call:

        A a;            // ctor A::A() called
        A b();          // function A(b)()

Claus
--
Claus Andre Faerber <claus@faerber.muc.de> <http://www.muc.de/~cfaerber/>
               PGP: finger <cfaerber@muc.de> / email <pgp@faerber.muc.de>
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: anhaeupl@late.e-technik.uni-erlangen.de (Bernd Anhaeupl)
Date: 1996/06/20
Raw View
In article <6B9pf5xZcDB@faerber.muc.de> claus@faerber.muc.de (Claus A. Faerber) writes:

   ## Morten Christensen <mor@dit.ou.dk> wrote on 17 Jun 96: ##

   > int main()
   > {
   >  A a;
   >  A b();

   Should the compiler treat this as a function prototype or as
   the creation of an object?!

   > - Output (Borland C++ 5.0) :
   >
   > Type of a : A
   > Type of b : A (*)()
   >
   > As this example shows, b is not an object but a pointer to a
   > function returning an A..... Not what I expected!

   This problem occurs with all ctors due to their syntax; the
   only solution is to have a look at the parameter list, if
   they are valid objects/variables/consts, it is a ctor call,
   if it consists of types, it is a function definition:

    A c(1);         // ctor A::A(int) called
    A d(int);       // function A(d)(int)

   But that can not work with no parameters, so the only
   solution then is to add the brackets for a function
   prototype and to omit them for a ctor call:

    A a;            // ctor A::A() called
    A b();          // function A(b)()

   Claus

see also [decl.fct.def], paragraph 9 (april 95 draft)


--
Bernd Anhaeupl   Tel.:  +49 9131 857787
LATE - Uni Erlangen
Cauerstr. 7   Email: anhaeupl@late.e-technik.uni-erlangen.de
91058 Erlangen
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]