Hello, I've been asked to port some old code to a new platform. We have
a vendor-supplied compiler that is supposed to be ansi compliant, but
it refuses to compile the following code:
$ cat test.c
int main(int argc, char **argv)
{
static int a, b, c, d, e, f, g, h, i, j, k;
return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
}
The error is 'ternary operation `?:` unterminated', however the GNU
compiler accepts it even in 'pedantic' mode. It's not a problem, as I
just rewrote the code, but I'm curious if this code (as i suspect)
looks acceptable to you language lawyers, if so I'll file a bug report
with the vendor.
Thanks guys. 5 2201 sl************* ***@hotmail.com wrote: Hello, I've been asked to port some old code to a new platform. We have a vendor-supplied compiler that is supposed to be ansi compliant, but it refuses to compile the following code:
$ cat test.c int main(int argc, char **argv) { static int a, b, c, d, e, f, g, h, i, j, k;
return k = a ? b++, c : d ? e++, f : g ? h++, i : j; }
The code above is valid and should compile properly. I happen to think
that the last statement is very poorly written though, I wouldn't want
to see the rest of the code written by the guy who came up with this
gem. This is the type of code that leads to bugs and maintenance
nightmares, especially if you forget that the tertiary operator is
right-to-left associative.
Robert Gamble
Robert Gamble wrote: sl************* ***@hotmail.com wrote:
Hello, I've been asked to port some old code to a new platform. We have a vendor-supplied compiler that is supposed to be ansi compliant, but it refuses to compile the following code:
$ cat test.c int main(int argc, char **argv) { static int a, b, c, d, e, f, g, h, i, j, k;
return k = a ? b++, c : d ? e++, f : g ? h++, i : j; }
The code above is valid and should compile properly. I happen to think that the last statement is very poorly written though, I wouldn't want to see the rest of the code written by the guy who came up with this gem. This is the type of code that leads to bugs and maintenance nightmares, especially if you forget that the tertiary operator is right-to-left associative.
Yeah. What he said. In spades.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/> sl************* ***@hotmail.com wrote: Hello, I've been asked to port some old code to a new platform. We have a vendor-supplied compiler that is supposed to be ansi compliant, but it refuses to compile the following code:
$ cat test.c int main(int argc, char **argv) { static int a, b, c, d, e, f, g, h, i, j, k;
return k = a ? b++, c : d ? e++, f : g ? h++, i : j; }
The error is 'ternary operation `?:` unterminated', however the GNU compiler accepts it even in 'pedantic' mode. It's not a problem, as I just rewrote the code, but I'm curious if this code (as i suspect) looks acceptable to you language lawyers, if so I'll file a bug report with the vendor.
Thanks guys.
You should send this in as obfuscated code, anybody
writing that kind of code, should not be allowed near
a computer,and waste his employers money. sl************* ***@hotmail.com wrote: Hello, I've been asked to port some old code to a new platform. We have a vendor-supplied compiler that is supposed to be ansi compliant, but it refuses to compile the following code:
$ cat test.c int main(int argc, char **argv) { static int a, b, c, d, e, f, g, h, i, j, k;
return k = a ? b++, c : d ? e++, f : g ? h++, i : j; }
The error is 'ternary operation `?:` unterminated', however the GNU compiler accepts it even in 'pedantic' mode. It's not a problem, as I just rewrote the code, but I'm curious if this code (as i suspect) looks acceptable to you language lawyers, if so I'll file a bug report with the vendor.
I would hazzard a guess that the compiler writer is not aware that the
middle
term of the conditional operator is any other expression...
conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression
In other words...
a ? b, c : d
....is equivalent to...
a ? (b, c) : d
--
Peter
Robert Gamble wrote: tertiary operator
There are unary, binary, and ternary operators,
according to the number of operands of the operator;
rather than primary, secondary, and tertiary.
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: zipher |
last post by:
It seems the debate over PEP 308 (if-then-else expression) occurred
prior to the arrival of generator expressions.
Mightn't this new latter syntax be the ticket to a "one obvious way"
to write a ternary expression in python?
>>> (foo(i) if i==42 else bar(i)) # i==42 ? foo(i) : bar(i)
zipher
|
by: Roger Leigh |
last post by:
This code works (GCC 3.3.3):
#include <iostream>
int main()
{
bool a = true;
std::cout << "True and false\n"
<< (a == true) ? 't' : 'f';
std::cout << "\n";
return 0;
|
by: glongword |
last post by:
As the assert macro should evaluate to a void expression, it should
not have an 'if statement' in its definition. Does this necessitate
the existence of a ternary conditional operator? Is there a way
assert.h can be implemented without using it? Are these decisions
related in anyway?
|
by: Nerox |
last post by:
Hi, If i write:
#include <stdio.h>
int foo(int);
int main(void){
int a = 3;
foo(a);
}
|
by: Robert Zurer |
last post by:
In his paper on Coding Standard found on
http://www.idesign.net/idesign/DesktopDefault.aspx
Juval Lowy discourages the use of the ternary conditional operator with no
specific reason given.
I can understand that complicated code would be much less readable using nested
operators, but aside from that is there another reason? -- something going on
under the hood perhaps which make it inadvisable to use?
| |
by: Daniel Crespo |
last post by:
Hi!
I would like to know how can I do the PHP ternary operator/statement
(... ? ... : ...) in Python...
I want to something like:
a = {'Huge': (quantity>90) ? True : False}
Any suggestions?
|
by: Udo A. Steinberg |
last post by:
Hi all,
In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed
that "x" will be equal to "a" only if the condition "cond" holds. Assuming that
"a" is a memory location, is it also guaranteed that "a" will not be accessed
in memory if the condition does not hold?
Or, in other words, is a compiler allowed to speculatively fetch a and b from
memory and then assign one of them to x based on the condition?
|
by: PerlPhi |
last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show you something first before i go to my delimma.
codes as follows using Mrs. If Else:
#!perl/bin/perl
use strict;
print "Are you sure you want to quit ('yes' or any key for 'no'): ";
chomp($_ = <STDIN>);
|
by: Sh4wn |
last post by:
Hi,
first, python is one of my fav languages, and i'll definitely keep
developing with it. But, there's 1 one thing what I -really- miss:
data hiding. I know member vars are private when you prefix them with
2 underscores, but I hate prefixing my vars, I'd rather add a keyword
before it.
Python advertises himself as a full OOP language, but why does it miss
one of the basic principles of OOP? Will it ever be added to python?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |