Topic: Declaration and initialization of built-in types using parenthesis


Author: yoder5@gate.net (Brian Yoder)
Date: Sun, 29 Sep 2002 16:12:00 +0000 (UTC)
Raw View
Yes, it appears that the Microsoft compiler's parser is somewhat less
than correct. Why an int built-in type is treated differently than an
"int *" (pointer to an int) built-in type makes no sense. But then
again, lots of MS sample code likes to typedef things like pFoo to be a
Foo * ( pointer to Foo). So maybe their compiler writers and testers get
sloppy because they never see a Foo* declaration and only expect the
pFoo declaration (for example).

What's a pain is the a LOT of code will need a lot of scattered
parenthesis so that MSC can compile it, even though it's perfectly legal
and correct code as it stands.

Brian

Victor Bazarov wrote:

>"Brian Yoder" <yoder5@gate.net> wrote...
>
>>I checked with the MSC users (as I don't have MSC installed nor do I use
>>it) and the issue is actually with pointer initialization and not with
>>my simple integer initialization as I was first told. For example, my
>>code compiles ok on Linux and AIX with the following, assuming that
>>my_str is an std::string object:
>>
>>   const char *s(my_str.c_str());
>>
>>However, it fails on MSC 6 unless the *s is surrounded by parenthesis,
>>as follows:
>>
>>   const char (*s)(my_str.c_str());
>>
>>The MSC error message I was shown seems to be written with some vague
>>reference to a standard but nothing that I could find.
>>
>
>Version 6 of VC++ makes no reference to the Standard in their
>documentation.  However, they do list error C2061 as a bug for
>the situation like described above (and that's what VC++ 6 gives
>as the error).  [Documents ## Q115705, Q113118]
>
>They also blow up if given an extra set of parentheses in the
>definition/initialisation:
>
>    const char *s((my_str.c_str()));
>
>VC++ 6 reports the second opening parenthesis as a syntax error.
>My guess is that they attempt to interpret the statement as a
>declaration of a function (because of parentheses after the
>identifier).
>
>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                       ]
>

---
[ 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: sachs@fnal.gov ("David Sachs")
Date: Sun, 29 Sep 2002 21:18:39 +0000 (UTC)
Raw View
"Brian Yoder" wrote...
>
> I checked with the MSC users (as I don't have MSC installed nor do I use
> it) and the issue is actually with pointer initialization and not with
> my simple integer initialization as I was first told. For example, my
> code compiles ok on Linux and AIX with the following, assuming that
> my_str is an std::string object:
>
>    const char *s(my_str.c_str());
>
> However, it fails on MSC 6 unless the *s is surrounded by parenthesis,
> as follows:
>
>    const char (*s)(my_str.c_str());

Don't forget that posfix operators (in this case function evaluation) bin
more strongly and prefix unarary operators (in this case indirection)


---
[ 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: Mon, 30 Sep 2002 17:39:42 +0000 (UTC)
Raw View
sachs@fnal.gov ("David Sachs") wrote in message
news:<an7nol$77a$1@info1.fnal.gov>...
> "Brian Yoder" wrote...

> > I checked with the MSC users (as I don't have MSC installed nor do I
> > use it) and the issue is actually with pointer initialization and
> > not with my simple integer initialization as I was first told. For
> > example, my code compiles ok on Linux and AIX with the following,
> > assuming that my_str is an std::string object:

> >    const char *s(my_str.c_str());

> > However, it fails on MSC 6 unless the *s is surrounded by
> > parenthesis, as follows:

> >    const char (*s)(my_str.c_str());

> Don't forget that posfix operators (in this case function evaluation)
> bin more strongly and prefix unarary operators (in this case
> indirection)

True, but in this case, there is no function evaluation operator in the
expression.  The (...) is an initializer, which has no effect on the
type.

Of course, this depends on the contents of the (...).  If those contents
can be interpreted as a declaration (or a list of declarations), then
the entire expression declares a function returning a char const* (or a
pointer to a function returning a char const, with the added
parentheses).  However, in this case, there is no way that the
expression can be interpreted as a declaration; the result is that the
(...) can only be an initializer.  (Determining this in the general case
requires infinite look-ahead, of course.)

--
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: sriram@ulticom.com (Sriram)
Date: Thu, 26 Sep 2002 18:34:03 +0000 (UTC)
Raw View
how abt constructor taking two arguments ?


Pete Becker wrote:

>Brian Yoder wrote:
>
>
>>And it makes it look
>>consistent with a class such as "my_int" that, if it had a ctor that
>>took an integer value, would be initialized as: my_int i(3);
>>
>>
>>
>
>It could also be initialized as: my_int i = 3;
>
>
>

---
[ 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: yoder5@gate.net (Brian Yoder)
Date: Tue, 24 Sep 2002 03:16:25 +0000 (UTC)
Raw View
I checked with the MSC users (as I don't have MSC installed nor do I use
it) and the issue is actually with pointer initialization and not with
my simple integer initialization as I was first told. For example, my
code compiles ok on Linux and AIX with the following, assuming that
my_str is an std::string object:

   const char *s(my_str.c_str());

However, it fails on MSC 6 unless the *s is surrounded by parenthesis,
as follows:

   const char (*s)(my_str.c_str());

The MSC error message I was shown seems to be written with some vague
reference to a standard but nothing that I could find.

Brian Yoder

---
[ 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: vAbazarov@dAnai.com ("Victor Bazarov")
Date: Tue, 24 Sep 2002 03:44:15 +0000 (UTC)
Raw View
"Brian Yoder" <yoder5@gate.net> wrote...
> I checked with the MSC users (as I don't have MSC installed nor do I use
> it) and the issue is actually with pointer initialization and not with
> my simple integer initialization as I was first told. For example, my
> code compiles ok on Linux and AIX with the following, assuming that
> my_str is an std::string object:
>
>    const char *s(my_str.c_str());
>
> However, it fails on MSC 6 unless the *s is surrounded by parenthesis,
> as follows:
>
>    const char (*s)(my_str.c_str());
>
> The MSC error message I was shown seems to be written with some vague
> reference to a standard but nothing that I could find.

Version 6 of VC++ makes no reference to the Standard in their
documentation.  However, they do list error C2061 as a bug for
the situation like described above (and that's what VC++ 6 gives
as the error).  [Documents ## Q115705, Q113118]

They also blow up if given an extra set of parentheses in the
definition/initialisation:

    const char *s((my_str.c_str()));

VC++ 6 reports the second opening parenthesis as a syntax error.
My guess is that they attempt to interpret the statement as a
declaration of a function (because of parentheses after the
identifier).

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: yoder5@gate.net (Brian Yoder)
Date: Sun, 22 Sep 2002 02:28:36 +0000 (UTC)
Raw View
I have been happily initializing built-in types such as ints using
the C++ object initialization syntax of parenthesis. Is this in the
C++ standard, or is it an extension that I shouldn't be depending
upon?

For example:

    int i(3);          // The way I've been coding it in C++

as opposed to:

    int i = 3;         // The C way

The former initialization makes an int look like a built-in class
instead of just a plain old C-style int. And it makes it look
consistent with a class such as "my_int" that, if it had a ctor that
took an integer value, would be initialized as: my_int i(3);

For my 8 years of writing C++, I have used a small but mixed set of
compilers such as Watcom (10 and 11), GCC (2.95), and the latest
version from IBM for AIX.

Those who have tried to compile the first example on MSC tell me
that the Microsoft compiler does not like this style for
initializaing built-in types. They tell me that it only accepts the
"int i=3" form, but not the "int i(3)" form.

Brian Yoder

---
[ 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: chris@trieagle.com ("Chris")
Date: Sun, 22 Sep 2002 21:02:32 +0000 (UTC)
Raw View
While I can't say for sure if it is in the standard or not, int i(3); works
fine on MSVC6.0

Chris
"Brian Yoder" <yoder5@gate.net> wrote in message
news:3D8CF092.4040000@gate.net...
> I have been happily initializing built-in types such as ints using
> the C++ object initialization syntax of parenthesis. Is this in the
> C++ standard, or is it an extension that I shouldn't be depending
> upon?
>
> For example:
>
>     int i(3);          // The way I've been coding it in C++
>
> as opposed to:
>
>     int i = 3;         // The C way
>
> The former initialization makes an int look like a built-in class
> instead of just a plain old C-style int. And it makes it look
> consistent with a class such as "my_int" that, if it had a ctor that
> took an integer value, would be initialized as: my_int i(3);
>
> For my 8 years of writing C++, I have used a small but mixed set of
> compilers such as Watcom (10 and 11), GCC (2.95), and the latest
> version from IBM for AIX.
>
> Those who have tried to compile the first example on MSC tell me
> that the Microsoft compiler does not like this style for
> initializaing built-in types. They tell me that it only accepts the
> "int i=3" form, but not the "int i(3)" form.
>
> Brian Yoder
>
> ---
> [ 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                       ]
>
>

---
[ 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: Sun, 22 Sep 2002 21:03:47 +0000 (UTC)
Raw View
yoder5@gate.net (Brian Yoder) wrote
> I have been happily initializing built-in types such as ints using
> the C++ object initialization syntax of parenthesis. Is this in the
> C++ standard, or is it an extension that I shouldn't be depending
> upon?
>
> For example:
>
>     int i(3);          // The way I've been coding it in C++
>
> as opposed to:
>
>     int i = 3;         // The C way

Both ways are standard.

> Those who have tried to compile the first example on MSC tell me
> that the Microsoft compiler does not like this style for
> initializaing built-in types. They tell me that it only accepts the
> "int i=3" form, but not the "int i(3)" form.

Just to be certain I wasn't crazy, I tested it in both Visual Studio 6.0
and Visual Studio .NET. I'm please to report that it worked correctly
in both compilers.

---
[ 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: cpdaniel@pacbell.net ("Carl Daniel")
Date: Mon, 23 Sep 2002 05:10:55 +0000 (UTC)
Raw View
"Brian Yoder" <yoder5@gate.net> wrote in message
news:3D8CF092.4040000@gate.net...
> I have been happily initializing built-in types such as ints using
> the C++ object initialization syntax of parenthesis. Is this in the
> C++ standard, or is it an extension that I shouldn't be depending
> upon?

Standard.

>
> Those who have tried to compile the first example on MSC tell me
> that the Microsoft compiler does not like this style for
> initializaing built-in types. They tell me that it only accepts the
> "int i=3" form, but not the "int i(3)" form.

They are mistaken.  Even VC6, which gets many things wrong, accepts the
"C++" style initialization.

-cd



---
[ 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: Mon, 23 Sep 2002 05:17:47 +0000 (UTC)
Raw View
In article <3D8CF092.4040000@gate.net>, Brian Yoder <yoder5@gate.net>
writes
>Those who have tried to compile the first example on MSC tell me
>that the Microsoft compiler does not like this style for
>initializaing built-in types. They tell me that it only accepts the
>"int i=3" form, but not the "int i(3)" form.

I think they are mistaken. In one context, that of ctor init lists, the
function style initialisation syntax is required.

--
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: loewis@informatik.hu-berlin.de (Martin v. =?iso-8859-1?q?L=F6wis?=)
Date: Mon, 23 Sep 2002 05:18:10 +0000 (UTC)
Raw View
yoder5@gate.net (Brian Yoder) writes:

> I have been happily initializing built-in types such as ints using
> the C++ object initialization syntax of parenthesis. Is this in the
> C++ standard, or is it an extension that I shouldn't be depending
> upon?

Your example is clearly conforming to the C++ standard, see 8.5/1, and
8.5/11.

Regards,
Martin

---
[ 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: cdm.henderson@virgin.net ("Craig Henderson")
Date: Mon, 23 Sep 2002 05:19:04 +0000 (UTC)
Raw View
"Brian Yoder" <yoder5@gate.net> wrote in message
news:3D8CF092.4040000@gate.net...

> Those who have tried to compile the first example on MSC tell me
> that the Microsoft compiler does not like this style for
> initializaing built-in types. They tell me that it only accepts the
> "int i=3" form, but not the "int i(3)" form.

The Microsoft Visual C++ Compiler certainly allows this syntax, and has for
several versions - At least since VC5, I think. Perhaps the MSC reference
means the Microsoft C Compiler, in which case, the syntax is not valid - it
is C++ only, not C.

Regards
-- Craig


---
[ 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: petebecker@acm.org (Pete Becker)
Date: Mon, 23 Sep 2002 14:36:29 +0000 (UTC)
Raw View
Brian Yoder wrote:
>
> And it makes it look
> consistent with a class such as "my_int" that, if it had a ctor that
> took an integer value, would be initialized as: my_int i(3);
>

It could also be initialized as: my_int i = 3;

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]