davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 10:06 am

hello,
is there any advantage for compiling by -std=c++11 currently ?
(which is the current gcc/g++ version provided by Jessie?)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 11:49 am

davenull wrote:Which is the current gcc/g++ version provided by jessie?
4.9.2
davenull wrote:Is there any advantage for compiling by -std=c++11 currently ?
I am assuming that your question is, "Given a valid C++03 program is there any advantage to compile using the g++ -std=c++11 flag?"

The program may gain some improved performance from the C++11. For a detailed discussion see Can modern C++ get you performance for free? on stackoverflow.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 4:42 pm

thanks for your reply!
Actually it was not just about speed but also about different features or advantages when using c11 instead of c2003, e.g. useful new functions or functionalities, important bug fixes, and so on.
E.g., eventually I have a program code which was recommended to be compiled by -std=c++11 although compilation also worked without it (no idea what makes a difference though, as both executables failed to run then accordingly).
Just computational speed is not so extremely important to me.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2972
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 6:03 pm

davenull wrote:Actually it was not just about speed but also about different features or advantages when using c11 instead of c2003, e.g. useful new functions or functionalities, important bug fixes, and so on.
E.g., eventually I have a program code which was recommended to be compiled by -std=c++11 although compilation also worked without it (no idea what makes a difference though, as both executables failed to run then accordingly).
Just computational speed is not so extremely important to me.
There are a fair few changes brought in C++11, https://en.wikipedia.org/wiki/C%2B%2B11 gives a decent breakdown of what the changes are.

And you shouldn't refer to C++11 as C11, they are different standards for different languages. Nor should you use C2003 when you mean C++03, though since C never had C03 (the previous was C99) it's less ambiguous.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 6:19 pm

ok, thank you, I didn't know that, I always assumed C was just a subset of C++.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2972
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 7:46 pm

davenull wrote:ok, thank you, I didn't know that, I always assumed C was just a subset of C++.
No, C++ is based on C (C++ was originally called "C with Classes"). C isn't a subset of C++ as you can write perfectly valid C code that will not compile under C++.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 8:19 pm

...perfectly valid C code that will not compile under C++.
do you have a source code example? 8-)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

jahboater
Posts: 5780
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: any advantage for compiling by -std=c++11 ?

Mon Apr 25, 2016 9:54 pm

The "restrict" keyword in C is not understood by C++.
Obviously C++ keywords should be avoided.
Not sure what else there is?
Pi4 8GB running PIOS64

User avatar
Paeryn
Posts: 2972
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: any advantage for compiling by -std=c++11 ?

Tue Apr 26, 2016 12:20 am

Code: Select all

void *func()
{
  return 0;
}

struct foo {
  int a;
};

typedef long foo;

int main()
{
  int old = 0;
  int new = 1;
  int *err = func();
  struct foo bar;
  foo humbug;

  bar.a = 4;
  foo = 5;
  printf("Hello\n");
  return 0;
}
That has four different errors that C++ will fail on.
1) new is a keyword in C++ so can't be used as a variable name.
2) C++ doesn't allow automatic pointer conversion.
3) C++ doesn't allow you to call undeclared functions.
4) in C++ struct and typedef have the same scope so the same identifier can't be used for each, in C they are separate.
She who travels light — forgot something.

yodermk
Posts: 35
Joined: Sat Mar 07, 2015 9:54 pm

Re: any advantage for compiling by -std=c++11 ?

Tue Apr 26, 2016 1:14 am

If you're compiling existing code, find out from the author which --std it requires, or try it with various options and see what works.

If you're writing new code, you absolutely should be using --std=c++11 or better yet --std=c++14 when it's available on the Pi. It's a night & day better language. *Please* don't write new C++ code with the pre-11 way of doing things (like manual pointers & such).

ejolson
Posts: 5409
Joined: Tue Mar 18, 2014 11:47 am

Re: any advantage for compiling by -std=c++11 ?

Tue Apr 26, 2016 2:44 am

jahboater wrote:The "restrict" keyword in C is not understood by C++.
Obviously C++ keywords should be avoided.
Not sure what else there is?
Almost all extensions to ANSI-C starting with C99 and then C11 are incompatible with C++. In addition to the already mentioned semantics involving pointer aliasing, the implementation of stack-allocated variable length arrays, complex numbers and declarations of counter variables in for loops are all done differently.

jahboater
Posts: 5780
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: any advantage for compiling by -std=c++11 ?

Tue Apr 26, 2016 7:48 am

ejolson wrote:Almost all extensions to ANSI-C starting with C99 and then C11 are incompatible with C++. In addition to the already mentioned semantics involving pointer aliasing, the implementation of stack-allocated variable length arrays, complex numbers and declarations of counter variables in for loops are all done differently.
declarations of counter variables in for loops are all done differently
"for( int i = 0; i < 50; ++i )" is the same - how is it different in C++?
already mentioned semantics involving pointer aliasing
I assume you mean:
"char * ptr = (char*)malloc()" - you don't need the cast in C but its perfectly valid.
Almost all extensions to ANSI-C starting with C99 and then C11 are incompatible with C++.
???? for example? in fact some of them are obviously intended to make the languages compatible.

How do stack allocated variable length arrays differ? They compile cleanly in both languages.

I admit I don't know about complex numbers in this respect.
Pi4 8GB running PIOS64

jahboater
Posts: 5780
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: any advantage for compiling by -std=c++11 ?

Tue Apr 26, 2016 8:02 am

Paeryn wrote:

Code: Select all

void *func()
{
  return 0;
}

struct foo {
  int a;
};

typedef long foo;

int main()
{
  int old = 0;
  int new = 1;
  int *err = func();
  struct foo bar;
  foo humbug;

  bar.a = 4;
  foo = 5;
  printf("Hello\n");
  return 0;
}
That has four different errors that C++ will fail on.
1) new is a keyword in C++ so can't be used as a variable name.
2) C++ doesn't allow automatic pointer conversion.
3) C++ doesn't allow you to call undeclared functions.
4) in C++ struct and typedef have the same scope so the same identifier can't be used for each, in C they are separate.
1) new is a keyword in C++ so can't be used as a variable name.
Obviously - as mentioned.

2) C++ doesn't allow automatic pointer conversion.
It does allow "int *err = (int*)func();" which is perfectly valid in both languages.

3) C++ doesn't allow you to call undeclared functions.
You wont get very far in C nowadays doing that. You get all sorts of warning messages that are hard or impossible to turn off. A well written C program has no problems.

4) in C++ struct and typedef have the same scope so the same identifier can't be used for each, in C they are separate.[/quote]That's a good point. Though easily avoided.
Pi4 8GB running PIOS64

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: any advantage for compiling by -std=c++11 ?

Tue Apr 26, 2016 8:05 am

ok, thanks to all, it's already quite a bunch of details which had to be considered, but some or even most of them are at a really sophisticated level which admittedly exceed my programming skills (currently).
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”