Is this syntactically valid C++?

A

Alan Mackenzie

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!
 
R

Rolf Magnus

Alan said:
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.
 
A

Alan Mackenzie

Hi, Rolf!

Rolf Magnus said:
Alan Mackenzie wrote:
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");
Syntactically, it seems correct, but the class is missing a constructor.

OK, Thanks!
They are instances of the class CLS.
Ah!
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.
 
Z

zoomzoom

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!

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");
 
Z

zoomzoom

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!

And cls_1 and cls_2 are intances of class CLS.
 
J

joe

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top