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

or vs || , and a template question

92 views
Skip to first unread message

JiiPee

unread,
Nov 18, 2014, 1:58:44 PM11/18/14
to
Is this forum also a place to ask simple questions? Sometimes does not
find answer for something.

Anyway here two:

1) New C++ has keywords "or" to replace ||. LIke:

int a = 6;
if(a == 5 or a > 9)
....

(old: if(a == 5 || a > 9) )

I really like "or" because its more readable. But is there something
against using it? Why C++ people normally still prefer ||, I don't see
people using "or" ? Is there a reason for it? I like to use all the
improvements as they come with C++ ...

2) I found a piece of code:

template <typename T, size_t N>
void foo( T(&arr)[N] )
{
// use arr - array...
}

and called:

int arr[] = {1,2,3};
foo(arr);

Been scratching my head with this...
I am not familiar with this syntax T(&arr)[N]. What is that? Why it
takes arr's address (&arr)? Or is it a reference? Also, is this a
template function argument which can automatically find T and N from
array arr when called?
Would like to know that this T(&arr)[N] means. I know N is the size
of the array, but the rest...

thanks

red floyd

unread,
Nov 18, 2014, 2:11:51 PM11/18/14
to
On 11/18/2014 10:58 AM, JiiPee wrote:

> 2) I found a piece of code:
>
> template <typename T, size_t N>
> void foo( T(&arr)[N] )
> {
> // use arr - array...
> }
>
> and called:
>
> int arr[] = {1,2,3};
> foo(arr);
>
> Been scratching my head with this...
> I am not familiar with this syntax T(&arr)[N]. What is that? Why it
> takes arr's address (&arr)? Or is it a reference? Also, is this a
> template function argument which can automatically find T and N from
> array arr when called?
> Would like to know that this T(&arr)[N] means. I know N is the size
> of the array, but the rest...

It's a reference to an array of N elements of type T.

A common usage is

template<typename T, size_t N>
size_t array_size(T(&arr)[N])
{
return N;
}



JiiPee

unread,
Nov 18, 2014, 2:31:23 PM11/18/14
to
On 18/11/2014 19:21, Martin Shobe wrote:
> On 11/18/2014 12:58 PM, JiiPee wrote:
>
>> 2) I found a piece of code:
>
>> template <typename T, size_t N>
>> void foo( T(&arr)[N] )
>> {
>> // use arr - array...
>> }
>
>> and called:
>
>> int arr[] = {1,2,3};
>> foo(arr);
>
>> Been scratching my head with this...
>> I am not familiar with this syntax T(&arr)[N]. What is that? Why it
>> takes arr's address (&arr)? Or is it a reference? Also, is this a
>> template function argument which can automatically find T and N from
>> array arr when called?
>> Would like to know that this T(&arr)[N] means. I know N is the size
>> of the array, but the rest...
>
> T (&arr)[N] is a reference to an array. N is the size of the array. T
> is the type of object in the array. Yes, the template function is
> automatically found when the call to foo is compiled.
>
> Martin Shobe
>

ok thanks

JiiPee

unread,
Nov 18, 2014, 2:35:47 PM11/18/14
to
template <typename T, size_t N>
void foo( T(&arr)[N] )
{
// use arr - array...
}

and called:

int arr[] = {1,2,3};
foo(arr);


On 18/11/2014 19:21, Martin Shobe wrote:
>
> Yes, the template function is automatically found when the call to
> foo is compiled.
>
>

I meant the foo function is able to automatically know T and N from the
call

foo(arr);

So obviously arr carries with it the information of its size and its
elements type I guess. Thats how foo know them.

JiiPee

unread,
Nov 18, 2014, 2:38:31 PM11/18/14
to
oh cool , this was new to me. The way to get the size of an array.


JiiPee

unread,
Nov 18, 2014, 2:47:00 PM11/18/14
to
On 18/11/2014 19:21, Martin Shobe wrote:
>
> Other than that, I continue to use || because it's what I expect to
> see when reading C++.

Ok, but do you agree that:

if( a == 5 or a == 8 )

is more human readable than

if( a == 5 || a == 8 )

?
we are really doing or-operation there, so how could something else than
or be better?

Scott Lurndal

unread,
Nov 18, 2014, 3:08:44 PM11/18/14
to
JiiPee <n...@notvalid.com> writes:
>On 18/11/2014 19:21, Martin Shobe wrote:
>>
>> Other than that, I continue to use || because it's what I expect to
>> see when reading C++.
>
>Ok, but do you agree that:
>
>if( a == 5 or a == 8 )
>
>is more human readable than
>
>if( a == 5 || a == 8 )
>

Having two ways to do the same thing in a single language wasn't
a good idea. In fact, this 'or' in C++ is a spectacularly bad idea.

Many of us don't have the option of using a newer compiler, or must
use many different versions of compilers (for various reasons) and
for that reason alone cannot (and will not) use 'or' in place of the
conditional-OR operator.

Rick C. Hodgin

unread,
Nov 18, 2014, 3:10:11 PM11/18/14
to
On Tuesday, November 18, 2014 2:47:00 PM UTC-5, JiiPee wrote:
> Ok, but do you agree that:
> if( a == 5 or a == 8 )
>
> is more human readable than:
> if( a == 5 || a == 8 )

I would say for most C/C++ developers, reading || and && are so
second nature now that it's not any easier to read. But for
newcomers to the language, and in moving forward, having the
or,and make sense.

I think there should also be xor, not, neg.

Best regards,
Rick C. Hodgin

Rick C. Hodgin

unread,
Nov 18, 2014, 3:12:46 PM11/18/14
to
I'm not sure it's a good idea to maintain like unto the past because
there are ways to do things in the present which are incompatible with
the way things were done in the past. I think the goal should always
be looking forward, not looking backward.

Wouter van Ooijen

unread,
Nov 18, 2014, 3:28:46 PM11/18/14
to
Martin Shobe schreef op 18-Nov-14 9:08 PM:
> Furthermore, "or"
> only means or in English (okay maybe there are other languages). Someone
> who speaks French might find using the word for gold rather odd. Symbols
> don't usually carry such baggage.

That hypotetical frenchman would have to get used to English anyway (if
then else do operator class template namespace throw catch ...).

Wouter van Ooijen
(Dutch)

JiiPee

unread,
Nov 18, 2014, 3:44:42 PM11/18/14
to
I agree. When I make code nowadays, am not trying make it so that it
would also compile with gnu 2.1 from 1995 :).

But obviously some are forced to do that.

I also agree that its not good to have two ways to do this thing. But
"or" should have been the first choice even 1985 ... imo.

JiiPee

unread,
Nov 18, 2014, 3:46:24 PM11/18/14
to
On 18/11/2014 20:08, Martin Shobe wrote:
> I've been using || for 27 years. or just recently became an option. ||
> is currently better (for me) because I'm used to it. Furthermore, "or"
> only means or in English (okay maybe there are other languages).
> Someone who speaks French might find using the word for gold rather
> odd. Symbols don't usually carry such baggage.
>
> Martin Shobe
>

but they use "or" in other languages.. for example visual basic uses
it... so it is a good candidate

JiiPee

unread,
Nov 18, 2014, 3:47:41 PM11/18/14
to
yes agree.
Also there are many other keywords also which are in english, like for,
do, ... almost all. So I do not see this argument being good in that way.

JiiPee

unread,
Nov 18, 2014, 3:50:43 PM11/18/14
to
"not" is there already :)

Scott Lurndal

unread,
Nov 18, 2014, 4:07:28 PM11/18/14
to
If you really want to use "OR" instead of "||", feel free to code
in COBOL or Fortran. In other words, nobody is looking backward
here except the C++11 committee.

Richard

unread,
Nov 18, 2014, 4:19:28 PM11/18/14
to
[Please do not mail me a copy of your followup]

n...@notvalid.com spake the secret code
<GmNaw.811873$PG2.8...@fx12.am4> thusly:

>On 18/11/2014 19:21, Martin Shobe wrote:
>Ok, but do you agree that:
>
>if( a == 5 or a == 8 )
>
>is more human readable than
>
>if( a == 5 || a == 8 )
>
>?

No. It isn't the norm in the community. (I would go farther and say
that writing if to look like a function call and bracketing the
conditional expression with additional whitespace also hinders
readability.)

>we are really doing or-operation there, so how could something else than
>or be better?

Section 2.6 of the standard calls these keywords "alternative tokens".
As far as I know, this is the only place in the standard where these
alternates are mentioned. Everywhere else logical OR is written as ||.
This is a strong hint that these alternate tokens aren't intended to
be the typical use.

The standard doesn't give a rationale for why these alternate tokens
are present in the language, but I suspect it is a legacy issue of
allowing C/C++ to be used in older environments where the keyboards
don't have some of the more "exotic" punctuation marks beyond those
found on typewriters.

Looking at the C standard, section 7.9 gives these same alternate
spellings as macros that expand to the operators. The macros are
defined in <iso646.h>. Looking up ISO 646 on Wikipedia gives a
description of this standard as the 7-bit coded character set -- in
other words it's the standardized version of ASCII.
<http://en.wikipedia.org/wiki/ISO/IEC_646>

Now take a look at the keyboard on a common teletype:
<http://commons.wikimedia.org/wiki/File:Mappa_Teletype_ASR-33.jpg>

Look carefully at the punctuation and you will find the following
characters are missing: {, }, |, ^ and ~. In such an environment
you would have no choice but to use the digraphs <%, %> for { and }
and the alternate keywords for operators containing ^, ~ and |.

So, you can see that these alternate spellings of the operators are
there only for compatibility with legacy environments and are not
considered to be used in "normal" situations.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Scott Lurndal

unread,
Nov 18, 2014, 4:34:55 PM11/18/14
to
legaliz...@mail.xmission.com (Richard) writes:
>[Please do not mail me a copy of your followup]
>
>n...@notvalid.com spake the secret code
><GmNaw.811873$PG2.8...@fx12.am4> thusly:
>
>>On 18/11/2014 19:21, Martin Shobe wrote:
>>Ok, but do you agree that:
>>
>>if( a == 5 or a == 8 )
>>
>>is more human readable than
>>
>>if( a == 5 || a == 8 )
>>
>>?
>
>No. It isn't the norm in the community. (I would go farther and say
>that writing if to look like a function call and bracketing the
>conditional expression with additional whitespace also hinders
>readability.)
>
>>we are really doing or-operation there, so how could something else than
>>or be better?
>
>Section 2.6 of the standard calls these keywords "alternative tokens".
>As far as I know, this is the only place in the standard where these
>alternates are mentioned. Everywhere else logical OR is written as ||.
>This is a strong hint that these alternate tokens aren't intended to
>be the typical use.
>

>Now take a look at the keyboard on a common teletype:
><http://commons.wikimedia.org/wiki/File:Mappa_Teletype_ASR-33.jpg>
>
>Look carefully at the punctuation and you will find the following
>characters are missing: {, }, |, ^ and ~. In such an environment
>you would have no choice but to use the digraphs <%, %> for { and }
>and the alternate keywords for operators containing ^, ~ and |.

Historically in C, this was handled by trigraphs '??(' == '[' etc.

In C++, 'or', 'and', 'xor', 'bitor', et. alia are considered by
the standard to be digraphs.

The term "digraph" (token consisting of two characters) is not perfectly
descriptive, since one of the alternative preprocessing-tokens is %:%:
and of course several primary tokens contain two characters. Nonetheless,
those alternative tokens that aren't lexical keywords are colloquially
known as "digraphs".

Jorgen Grahn

unread,
Nov 18, 2014, 4:38:41 PM11/18/14
to
On Tue, 2014-11-18, Martin Shobe wrote:
> On 11/18/2014 1:46 PM, JiiPee wrote:
> I've been using || for 27 years. or just recently became an option.

Because you used some other language until recently, surely? Because
'or' has been in the language since at least 1994 -- that's 20 years!

Not that I use 'or' and friends myself -- that's an area where
deviating from C is more trouble than it's worth. And noone else
uses them ...

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

JiiPee

unread,
Nov 18, 2014, 4:40:36 PM11/18/14
to
On 18/11/2014 21:19, Richard wrote:
> [Please do not mail me a copy of your followup]
>
>
> So, you can see that these alternate spellings of the operators are
> there only for compatibility with legacy environments and are not
> considered to be used in "normal" situations.

Looks like that is the case ... unfortunately :(. I still think or is
better, but if everybody else uses || what can I do?...seems like I am
forced to go back to || .


Öö Tiib

unread,
Nov 18, 2014, 5:03:27 PM11/18/14
to
On Tuesday, 18 November 2014 20:58:44 UTC+2, JiiPee wrote:
> Is this forum also a place to ask simple questions? Sometimes does not
> find answer for something.
>
> Anyway here two:
>
> 1) New C++ has keywords "or" to replace ||. LIke:
>
> int a = 6;
> if(a == 5 or a > 9)
> ....
>
> (old: if(a == 5 || a > 9) )

In some sense of "new". The keywords 'and', 'and_eq', 'bitand',
'bitor', 'compl', 'not', 'or', 'or_eq' and so on were there in
C++03 as well.

> I really like "or" because its more readable. But is there something
> against using it? Why C++ people normally still prefer ||, I don't see
> people using "or" ? Is there a reason for it? I like to use all the
> improvements as they come with C++ ...

With too lot of operator and punctuation keywords the program code
becomes too COBOL-like for my taste. Even Pascal and Ada feel too
lot of keywords. I configure code editor to make operators of
different color (for example bold dark grey) to ease reading such
languages. That is matter of taste perhaps but since so lot of
popular programming languages share the syntactic style of C
(Java, Javascript, C++, C#, lua and so on and so on) I suspect
that lot of programmers share my view on syntax.

I think of the keywords as better alternative tokens than trigraphs
for those unlucky who do not have access to keyboard with all symbols.
So ... 'compl b' is certainly better than '??- b' but '~b' is best
for me to read. Similarly 'a xor b' is better than 'a ??' b'
but 'a ^ b' is best.


Richard

unread,
Nov 18, 2014, 5:42:56 PM11/18/14
to
[Please do not mail me a copy of your followup]

sl...@pacbell.net spake the secret code
<UXOaw.723478$FX2.4...@fx18.iad> thusly:

>Historically in C, this was handled by trigraphs '??(' == '[' etc.

Trigraphs are another mechanism for representing some of the same
characters. Trigraphs are listed in section 5.2.1.1, paragraph 1
of the C standard.

The current version of the C standard also describes digraphs. See
section 6.4.6, paragraph 3.

>In C++, 'or', 'and', 'xor', 'bitor', et. alia are considered by
>the standard to be digraphs.

Sorry, but they're not. They're called alternative tokens or reserved
words in section 2.6, paragraph 1.

> The term "digraph" (token consisting of two characters) is not perfectly
> descriptive, since one of the alternative preprocessing-tokens is %:%:
> and of course several primary tokens contain two characters. Nonetheless,
> those alternative tokens that aren't lexical keywords are colloquially
> known as "digraphs".

This is the footnote in section 2.6 and it is not referring to 'or',
'and', etc., in the table of alternative tokens, but to <%, %>, <:, :>,
%:, %:%: in the table.

You omitted the first sentence of the footnote that makes this
abundantly clear:

"These include "digraphs" and additional reserved words."

None of this is to be confused with trigraph sequences described in
section 2.4 which also provide a means of typing some of the more
"exotic" punctuation not found on older input devices.

Alain Ketterlin

unread,
Nov 18, 2014, 6:06:14 PM11/18/14
to
JiiPee <n...@notvalid.com> writes:

> On 18/11/2014 19:21, Martin Shobe wrote:
>>
>> Other than that, I continue to use || because it's what I expect to
>> see when reading C++.
>
> Ok, but do you agree that:
>
> if( a == 5 or a == 8 )
>
> is more human readable than
>
> if( a == 5 || a == 8 )
>
> ?

No. I think using "or" is actually misleading, I would prefer "orelse"
as in Standard ML.

> we are really doing or-operation there, so how could something else
> than or be better?

Anything else is better. "||" does not commute, because of
short-circuiting rules. Its meaning is significantly different from "or"
(the grammatical conjunction), from "or" (the logical connective), from
"|" (the bitwise-or operator), and from what you call the "or-operation"
(I don't know what this is)--all of these being named or. It makes a lot
of sense to have a special symbol. I think the C commitee was wise to
require inclusion of iso646.h to use or, it is a pity C++ made them
keywords.

-- Alain.

Richard

unread,
Nov 18, 2014, 6:30:06 PM11/18/14
to
[Please do not mail me a copy of your followup]

Alain Ketterlin <al...@dpt-info.u-strasbg.fr> spake the secret code
<87bno4n...@dpt-info.u-strasbg.fr> thusly:

>[...] "||" does not commute, because of
>short-circuiting rules. Its meaning is significantly different from "or"
>(the grammatical conjunction), from "or" (the logical connective), from
>"|" (the bitwise-or operator), and from what you call the "or-operation"
>(I don't know what this is)--all of these being named or.

The standard uses "bitwise OR" for | and "logical OR" for ||.

Louis Krupp

unread,
Nov 19, 2014, 12:50:28 AM11/19/14
to
On Tue, 18 Nov 2014 18:58:31 +0000, JiiPee <n...@notvalid.com> wrote:

>Is this forum also a place to ask simple questions? Sometimes does not
>find answer for something.
>
>Anyway here two:
>
>1) New C++ has keywords "or" to replace ||. LIke:
>
>int a = 6;
>if(a == 5 or a > 9)
> ....
>
>(old: if(a == 5 || a > 9) )
>
>I really like "or" because its more readable. But is there something
>against using it? Why C++ people normally still prefer ||, I don't see
>people using "or" ? Is there a reason for it? I like to use all the
>improvements as they come with C++ ...

I personally would rather not use it. Perl has both "||" and "or"
operators. They do similar things, but they have different
precedence, and I usually have a reason to use one or the other.

Louis

Juha Nieminen

unread,
Nov 19, 2014, 3:35:40 AM11/19/14
to
Scott Lurndal <sc...@slp53.sl.home> wrote:
> Many of us don't have the option of using a newer compiler, or must
> use many different versions of compilers (for various reasons) and
> for that reason alone cannot (and will not) use 'or' in place of the
> conditional-OR operator.

You do realize that 'or' isn't a "new" feature? It's C++98.

If you can't use C++98 because you don't have a "newer compiler",
then perhaps you shouldn't be coding in C++ in the first place.
We don't live in the 90's anymore.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

David Brown

unread,
Nov 19, 2014, 3:38:27 AM11/19/14
to
On 18/11/14 21:08, Martin Shobe wrote:
> On 11/18/2014 1:46 PM, JiiPee wrote:
> I've been using || for 27 years. or just recently became an option. ||
> is currently better (for me) because I'm used to it. Furthermore, "or"
> only means or in English (okay maybe there are other languages). Someone
> who speaks French might find using the word for gold rather odd. Symbols
> don't usually carry such baggage.
>
> Martin Shobe
>

"or" in English also normally "exclusive or" (as in, "would you like an
apple /or/ a banana?"), while "or" in programming usually means
"inclusive or", allowing for both to be "true". So from this viewpoint,
using "or" rather than || here does not actually make the code more
readable.

JiiPee

unread,
Nov 19, 2014, 4:15:01 AM11/19/14
to
But I never thought about programming language being like normal
language but rather like a mathermatical language and in mathematics
'or' means exatcly the same as in programming (inclusive). So for me
this is not a problem.

Also, even in normal english "or" is also inclusive sometimes. For example:
"Paul, if the delivery arrives before 2pm or John calls before 2pm
please send me an email at 4pm". Now if both happens surely Paul should
send that email, isn't it?

Reinhardt Behm

unread,
Nov 19, 2014, 5:33:04 AM11/19/14
to
JiiPee wrote:

> But I never thought about programming language being like normal
> language but rather like a mathermatical language and in mathematics
> 'or' means exatcly the same as in programming (inclusive). So for me
> this is not a problem.
>
> Also, even in normal english "or" is also inclusive sometimes. For
> example: "Paul, if the delivery arrives before 2pm or John calls before
> 2pm please send me an email at 4pm". Now if both happens surely Paul
> should send that email, isn't it?

Not if he is a programmer. ;-)

--
Reinhardt

Jorgen Grahn

unread,
Nov 19, 2014, 4:02:04 PM11/19/14
to
On Wed, 2014-11-19, Martin Shobe wrote:
> On 11/18/2014 3:38 PM, Jorgen Grahn wrote:
>> On Tue, 2014-11-18, Martin Shobe wrote:
...
>>> I've been using || for 27 years. or just recently became an option.
>>
>> Because you used some other language until recently, surely? Because
>> 'or' has been in the language since at least 1994 -- that's 20 years!
>>
>> Not that I use 'or' and friends myself -- that's an area where
>> deviating from C is more trouble than it's worth. And noone else
>> uses them ...
>
> I've been using C and C++ during that time period. I just wasn't aware
> of the or token until recently.

Ah, then I understand. Yes, it /is/ surprisingly unknown.
0 new messages