Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ comma operator ?

42 views
Skip to first unread message

Lynn McGuire

unread,
Sep 30, 2022, 12:48:38 AM9/30/22
to
Is this C++ statement using the comma operator ?

write(6, "(3(1x,i3),1x,f12.6,1x,f12.6)"), hkl(1, i), hkl(2, i),
hkl(3, i), f_calc(1, i), f_calc(2, i);

There is a large open source template library supporting this statement
from Fortran to C++ Fable software.

Thanks,
Lynn

Lynn McGuire

unread,
Sep 30, 2022, 1:02:16 AM9/30/22
to
If interested, you can see the Fable FEM (Fortran Emulation) code at:
https://cci.lbl.gov/fable/

Lynn



Lynn McGuire

unread,
Sep 30, 2022, 1:03:35 AM9/30/22
to

James Kuyper

unread,
Sep 30, 2022, 1:06:12 AM9/30/22
to
If write, hkl, and f_calc are macros, you cannot be sure without
examining the macro definitions. However, if any of those three is an
ordinary function, it seems to me that this statement does unnecessarily
use the comma operator.

red floyd

unread,
Sep 30, 2022, 1:46:10 AM9/30/22
to
Not to mention that write() has an incorrect number of parameters,
assuming POSIX.

Gawr Gura

unread,
Sep 30, 2022, 2:19:51 AM9/30/22
to
Assuming you're looking at the same example file on the fable web page
as I am (sf.cpp at line 113) then I think the answer is yes. I believe
this is a series of calls to fem::write_loop::operator,()

Scott Lurndal

unread,
Sep 30, 2022, 11:16:21 AM9/30/22
to
The write here is an analog of the fortran write,

WRITE unit,format,iolist

And yes, it will conflict with the Unix (and POSIX) write function signature.

Lynn McGuire

unread,
Sep 30, 2022, 2:31:52 PM9/30/22
to
Yes, that does seem to be true.

Thanks,
Lynn

Lynn McGuire

unread,
Sep 30, 2022, 2:32:48 PM9/30/22
to
hkl and f_calc are two dimensioned arrayed objects.

Thanks,
Lynn


Lynn McGuire

unread,
Sep 30, 2022, 2:53:54 PM9/30/22
to
Yes, the Fortran write is "WRITE (unit, format) var1, var2, var3, etc..."
https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnbs/index.html

And yes, I am wondering if I will have to use the abhorrent "using
namespace fem;" statement to make sure that I get the correct write
function (which is no guarantee at all). Or, if I will have to use
"fem::write".

Thanks,
Lynn


Keith Thompson

unread,
Sep 30, 2022, 3:42:12 PM9/30/22
to
Lynn McGuire <lynnmc...@gmail.com> writes:
> Is this C++ statement using the comma operator ?
>
> write(6, "(3(1x,i3),1x,f12.6,1x,f12.6)"), hkl(1, i), hkl(2, i),
> hkl(3, i), f_calc(1, i), f_calc(2, i);

Yes, five of them, easily seen by reformatting the code:

write(6, "(3(1x,i3),1x,f12.6,1x,f12.6)"),
hkl(1, i),
hkl(2, i),
hkl(3, i),
f_calc(1, i),
f_calc(2, i);

Assuming there's no macro funny business going on, that's 6 function
calls (or macro invocations?) separated by comma operators. (The commas
separating function arguments are of course not comma operators.)

Replacing the comma at the end of each line by a semicolon (and possibly
surrounding the whole thing with curly braces) yields equivalent code.
The braces are needed if the context is something like:

if (condition)
write(6, "(3(1x,i3),1x,f12.6,1x,f12.6)"), hkl(1, i), hkl(2, i),
hkl(3, i), f_calc(1, i), f_calc(2, i);

Things might change if any of write, hkl, and f_calc is a macro. If
they're well behaved macros, it shouldn't matter.

> There is a large open source template library supporting this
> statement from Fortran to C++ Fable software.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Gawr Gura

unread,
Sep 30, 2022, 4:17:49 PM9/30/22
to
> And yes, I am wondering if I will have to use the abhorrent "using
> namespace fem;" statement to make sure that I get the correct write
> function (which is no guarantee at all).  Or, if I will have to use
> "fem::write".

The write symbol in the example program is an object of the type
fem::common_writer (you can see the declaration on line 70). Obviously,
you will have to name the type explicitly, provide a using declaration,
or rely on type inference when you declare a value of that type. write
is just the name of the object though so you could call it anything you
like. Perhaps:

fem::common_writer gregory(cmn);

or

fem::common_writer louise(cmn);

If you want to be able to use the POSIX write function in the same
program you could get it with ::write rather than just write.

Manfred

unread,
Sep 30, 2022, 7:03:59 PM9/30/22
to
"unnecessarily" is not necessarily true here: others have mentioned an
overloaded comma operator.
/If/ this is what is actually going on here, then you might need to keep
the sequence of commas in place, or go through some nontrivial rewrite.


Lynn McGuire

unread,
Oct 1, 2022, 12:09:43 AM10/1/22
to
As time goes on, some of these write statements will be rewritten in
conventional c++ code but for now, the Fortran like expression will be
good enough.

Thanks,
Lynn

Lynn McGuire

unread,
Oct 1, 2022, 12:48:24 AM10/1/22
to
On 9/29/2022 11:48 PM, Lynn McGuire wrote:
I just noted that f2c is using the comma operator in translated
expressions also, original fortran code:

test = abs( sienth(1) - vdy(adia_sienth_ptr+i) )

New C/C++ code:

test = (d__1 = sienth[1] - vdyn1.vdy[adia_sienth_ptr + i - 1],
abs(d__1));

Interesting. Can abs() under C/C++ not handle the expression inside it
? That does not make sense.

Thanks,
Lynn


Scott Lurndal

unread,
Oct 1, 2022, 12:56:56 PM10/1/22
to
Given C++ supports function signature overloads, I doubt you'll have
a problem with this in particular unless you only have a single element
iolist. (note that in F77, there were no parentheses in the WRITE statement).

Lynn McGuire

unread,
Oct 1, 2022, 2:13:53 PM10/1/22
to
I suspect that you are thinking of the Fortran PRINT statement.
https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnap/index.html

The Fortran WRITE statement has always had parenthesis for the i/o unit
and the format.
https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnbs/index.html

Lynn

Lynn McGuire

unread,
Oct 1, 2022, 3:30:24 PM10/1/22
to
> Subscripted using hkl(3,i)? I thought this was supposed to be C++
code, not Fortran?

They are C++ objects acting like Fortran arrays.

Lynn


James Kuyper

unread,
Oct 2, 2022, 4:09:10 PM10/2/22
to
They have an object type with an operator() overload that takes the
indices for the array access? If so, I think that describing them as
"arrayed objects" is a bit misleading.

Tim Rentsch

unread,
Oct 3, 2022, 5:33:01 AM10/3/22
to
sc...@slp53.sl.home (Scott Lurndal) writes:

> Given C++ supports function signature overloads,

What C++ supports is function name overloading (and also operator
overloading). "Function signature overloads" is a meaningless
phrase.

Tim Rentsch

unread,
Oct 3, 2022, 5:39:16 AM10/3/22
to
Keith Thompson <Keith.S.T...@gmail.com> writes:

> Lynn McGuire <lynnmc...@gmail.com> writes:
>
>> Is this C++ statement using the comma operator ?
>>
>> write(6, "(3(1x,i3),1x,f12.6,1x,f12.6)"),
>> hkl(1, i), hkl(2, i), hkl(3, i), f_calc(1, i), f_calc(2, i);
>
> Yes, five of them, easily seen by reformatting the code:
>
> write(6, "(3(1x,i3),1x,f12.6,1x,f12.6)"),
> hkl(1, i),
> hkl(2, i),
> hkl(3, i),
> f_calc(1, i),
> f_calc(2, i);
>
> Assuming there's no macro funny business going on, that's 6
> function calls (or macro invocations?) separated by comma
> operators. (The commas separating function arguments are of
> course not comma operators.)
>
> Replacing the comma at the end of each line by a semicolon (and
> possibly surrounding the whole thing with curly braces) yields
> equivalent code.

In C++ that rule does not hold, because of the possibility of an
overloaded operator,(). Indeed in this case it appears that
there is an operator,() overload, and that is what makes this
statement work; changing the statement to be a series of
semicolon-terminated statements wouldn't work at all.

Juha Nieminen

unread,
Oct 3, 2022, 6:48:43 AM10/3/22
to
"Function signature overload" would imply that two functions with the
same signature (ie. not only the same name, but the same amount of
parameters, each being of the exact same type, and the same visibility
scope) could be someone "overloaded". Which is of course not possible.

(Technically speaking you could try that with inline functions, but
having more than one different implemenation for a particular inline
function signature is quite explicitly UB in the standard.)
0 new messages