Is this syntactically valid C++?

Discussion in 'C++' started by Alan Mackenzie, Aug 14, 2010.

  1. Here is a fragment from an old test case in Emacs's C++ Mode:

    1 class CLS {
    2 int i; float f; char *s;
    3 }
    4 cls_1 /* ; */ (1,
    5 1.0,
    6 "foo"
    7 ),
    8 cls_2 (2, -1.0, "bar");

    .. Noting that the bracketing characters on lines 4, 7 and 8 are
    parentheses (not braces), is this coherent C++? If so, what sort of
    identifiers are "cls_1" and "cls_2"?

    At the moment, Emacs's C++ mode is highlighting cls_1 and cls_2 as
    functions. If the fragment is coherent C++, this might be the wrong
    thing to do. On the other hand, If the fragment is nonsense, it doesn't
    really matter.

    Please, somebody, help me out with this puzzling bit of code. Thanks in
    advance!

    --
    Alan Mackenzie (Nuremberg, Germany).
     
    Alan Mackenzie, Aug 14, 2010
    #1
    1. Advertising

  2. Alan Mackenzie

    Rolf Magnus Guest

    Alan Mackenzie wrote:

    > Here is a fragment from an old test case in Emacs's C++ Mode:
    >
    > 1 class CLS {
    > 2 int i; float f; char *s;
    > 3 }
    > 4 cls_1 /* ; */ (1,
    > 5 1.0,
    > 6 "foo"
    > 7 ),
    > 8 cls_2 (2, -1.0, "bar");


    Let's write this without the irritating comments and line breaks:

    class CLS {
    int i; float f; char *s;
    }
    cls_1 (1, 1.0, "foo"),
    cls_2 (2, -1.0, "bar");

    which is the same as:

    class CLS {
    int i; float f; char *s;
    };

    CLS cls_1 (1, 1.0, "foo");
    CLS cls_2 (2, -1.0, "bar");


    > . Noting that the bracketing characters on lines 4, 7 and 8 are
    > parentheses (not braces), is this coherent C++?


    Syntactically, it seems correct, but the class is missing a constructor.

    > If so, what sort of
    > identifiers are "cls_1" and "cls_2"?


    They are instances of the class CLS.

    > At the moment, Emacs's C++ mode is highlighting cls_1 and cls_2 as
    > functions. If the fragment is coherent C++, this might be the wrong
    > thing to do.


    I don't see how cls_1 and cls_2 could be functions. In a function call, you
    don't specify a return type, and in a function declaration, you need to
    specify argument types, not values.
     
    Rolf Magnus, Aug 14, 2010
    #2
    1. Advertising

  3. Hi, Rolf!

    Rolf Magnus <> wrote:
    > Alan Mackenzie wrote:


    >> Here is a fragment from an old test case in Emacs's C++ Mode:


    >> 1 class CLS {
    >> 2 int i; float f; char *s;
    >> 3 }
    >> 4 cls_1 /* ; */ (1,
    >> 5 1.0,
    >> 6 "foo"
    >> 7 ),
    >> 8 cls_2 (2, -1.0, "bar");


    > Let's write this without the irritating comments and line breaks:


    > class CLS {
    > int i; float f; char *s;
    > }
    > cls_1 (1, 1.0, "foo"),
    > cls_2 (2, -1.0, "bar");


    > which is the same as:


    > class CLS {
    > int i; float f; char *s;
    > };


    > CLS cls_1 (1, 1.0, "foo");
    > CLS cls_2 (2, -1.0, "bar");



    >> . Noting that the bracketing characters on lines 4, 7 and 8 are
    >> parentheses (not braces), is this coherent C++?


    > Syntactically, it seems correct, but the class is missing a constructor.


    OK, Thanks!

    >> If so, what sort of identifiers are "cls_1" and "cls_2"?


    > They are instances of the class CLS.


    Ah!

    >> At the moment, Emacs's C++ mode is highlighting cls_1 and cls_2 as
    >> functions. If the fragment is coherent C++, this might be the wrong
    >> thing to do.


    > I don't see how cls_1 and cls_2 could be functions.


    They were (naively) recognised as functions because of the open
    parenthesis (not brace) following the identifier.

    > In a function call, you don't specify a return type, and in a function
    > declaration, you need to specify argument types, not values.


    Looks like I've got some work to do to highlight these identifiers as
    variables.

    Thanks for such a quick and helpful reply.

    --
    Alan Mackenzie (Nuremberg, Germany).
     
    Alan Mackenzie, Aug 14, 2010
    #3
  4. Alan Mackenzie

    zoomzoom Guest

    On Aug 14, 12:02 pm, Alan Mackenzie <> wrote:
    > Here is a fragment from an old test case in Emacs's C++ Mode:
    >
    > 1 class CLS {
    > 2     int i; float f; char *s;
    > 3 }
    > 4     cls_1 /* ; */ (1,
    > 5                    1.0,
    > 6                    "foo"
    > 7         ),
    > 8     cls_2 (2, -1.0, "bar");
    >
    > .  Noting that the bracketing characters on lines 4, 7 and 8 are
    > parentheses (not braces), is this coherent C++?  If so, what sort of
    > identifiers are "cls_1" and "cls_2"?
    >
    > At the moment, Emacs's C++ mode is highlighting cls_1 and cls_2 as
    > functions.  If the fragment is coherent C++, this might be the wrong
    > thing to do.  On the other hand, If the fragment is nonsense, it doesn't
    > really matter.
    >
    > Please, somebody, help me out with this puzzling bit of code.  Thanks in
    > advance!
    >
    > --
    > Alan Mackenzie (Nuremberg, Germany).


    Sorry it is not a correct C++ fragment; There is no CLS constructor
    which takes an int, float and const char * as arguments. The C++
    compiler will only generate a the no argument and copy constructor
    which respectively takes no arguments and a reference to a CLS
    instance, i.e. CLS::CLS() and CLS::CLS(const CLS &). The intent may
    have been to do the following

    class CLS {
    int i, float f, char *s;
    // Note the following 3 lines
    public:
    CLS(int _i, float _f, char *_s): i(_i), f(_f), s(_s){}
    } cls_1(1, 1.0, "foo"),
    cls_2(2, -1.0, "bar");
     
    zoomzoom, Aug 14, 2010
    #4
  5. Alan Mackenzie

    zoomzoom Guest

    On Aug 14, 12:02 pm, Alan Mackenzie <> wrote:
    > Here is a fragment from an old test case in Emacs's C++ Mode:
    >
    > 1 class CLS {
    > 2     int i; float f; char *s;
    > 3 }
    > 4     cls_1 /* ; */ (1,
    > 5                    1.0,
    > 6                    "foo"
    > 7         ),
    > 8     cls_2 (2, -1.0, "bar");
    >
    > .  Noting that the bracketing characters on lines 4, 7 and 8 are
    > parentheses (not braces), is this coherent C++?  If so, what sort of
    > identifiers are "cls_1" and "cls_2"?
    >
    > At the moment, Emacs's C++ mode is highlighting cls_1 and cls_2 as
    > functions.  If the fragment is coherent C++, this might be the wrong
    > thing to do.  On the other hand, If the fragment is nonsense, it doesn't
    > really matter.
    >
    > Please, somebody, help me out with this puzzling bit of code.  Thanks in
    > advance!
    >
    > --
    > Alan Mackenzie (Nuremberg, Germany).


    And cls_1 and cls_2 are intances of class CLS.
     
    zoomzoom, Aug 14, 2010
    #5
  6. Alan Mackenzie

    joe Guest

    I select the OP's subjectline (remember when we were young (?): subject
    line (2 words, not one), THOSE were the days!). By "definition", you're
    old when you don't care about whether your parenthesis all close
    correctly. Actually, parenthesis are for oldsters! "Youth is wasted on
    the young". (Unless you are a celeb, of course, then .. nevermind).

    "Are all the parenthesis in the above OK?" == "Is this .. blah, blah".
    It's always the same day, I assure you, and tomorrow the same song will
    play on the clock radio.
     
    joe, Aug 15, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aschel Kritsos

    Valid file types

    Aschel Kritsos, Nov 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    598
  2. Ajit
    Replies:
    1
    Views:
    3,614
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  3. CT

    vbc is not valid command

    CT, Oct 30, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,844
  4. Replies:
    6
    Views:
    358
    CBFalconer
    Feb 28, 2007
  5. Christopher Benson-Manica

    Is this syntactically valid Java?

    Christopher Benson-Manica, Nov 7, 2007, in forum: Java
    Replies:
    16
    Views:
    647
    Daniel Pitts
    Nov 29, 2007
Loading...

Share This Page