Constexpr

Apr 23, 2017 at 5:01am
I've been working out of Sam's C++ book. Everything is going well, however I've gotten stuck with the keyword 'constexpr'. I understand the purpose and concept. But whenever I try to run it in a program there is always a bug even with the book example. Could someone please help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
using namespace std;

constexpr int Square(int number){ return number*number; }

int main(){
	const int ARRAY_LENGTH = 5;

	int myNumbers [ARRAY_LENGTH] = {5, 10, 0, -101, 20};
	int moreNumbers [Square(ARRAY_LENGTH)];

	cout << " Enter Index of the element to be changed: ";

	int elementIndex = 0;
	cin >> elementIndex;

	cout << "Enter new value: ";
	int newValue = 0;
	cin >> newValue;

	myNumbers [elementIndex] = newValue;
	moreNumbers [elementIndex] = newValue;

	cout << "Element " << elementIndex << "in array myNumbers is: ";
	cout << myNumbers[elementIndex] << endl;

	cout << "Element " <<elementIndex << "in array moreNumbers is: ";
	cout << moreNumbers[elementIndex] << endl;

	return 0;
}
Last edited on Apr 23, 2017 at 5:02am
Apr 23, 2017 at 5:09am
Why do you think there's a bug? This example doesn't seem to relate to constexpr.
Apr 23, 2017 at 5:40am
@kbw First I would like to thank you for replying so quickly, I really appreciate your help. & That's what I was thinking. This is a problem straight from the book. I like to go over the examples when I get down reading to grasp as much as I can. The two examples on how the book uses 'constexpr' I get a bug every time. here is the other example from the book where I get a bug using 'constexpr'. Like I said earlier I totally understand 'constexpr', but I'm confused on how to use it. Could you or anyone refer me to an example. I've looked around on the forum, but the examples I've ran across are a little too complex for what I know about c++ at the moment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
constexpr double GetPi() { return 22.0 / 7; }
constexpr double TwicePi() { return 2 * GetPi(); }

int main()
{
   using namespace std;
   const double pi = 22.0 / 7;

   cout << "constant pi contains value " << pi << endl;
   cout << "constexpr GetPi() returns value " << GetPi() << endl;
   cout << "constexpr TwicePi() returns value " << TwicePi() << endl;

   return 0;
}
Apr 23, 2017 at 6:29am
Hi,

What do you mean by contains a bug? A compiler warning / error ; unexpected behaviour at run time?

The code compiles on cpp.sh
Apr 23, 2017 at 7:55am
constexpr (keyword) requires C++11.
If you are using a very old compiler, switch to a more recent one.

In addition, if it is the GNU or a GNU-compatible compiler, by default it compiles a home-grown Linux dialect based on of the superseded C++ standard. We would need to explicitly ask for standard C++ with
-std=c++14 and -pedantic-errors (yes, say it twice; both options are required).
Apr 23, 2017 at 1:48pm
Sorry, I didn't realize you meant compile error when you said bug. To my mind, they're different things.

If you're not using C++11, you will get syntax errors, as that's when the feature was introduced.
Apr 23, 2017 at 11:00pm
@TheldeasMan @kbw @JLBorges Sorry I confused you guise with the wrong word usage. I'm currently using Eclipse 3.8 on a Lunix OS, I thought it came with all the bells ans whistles as far as supporting c++11 & 14. Is there a way I can find what compiler I'm using through the IDE and also how do I go about updating it? Or better yet do you recommend another IDE for a beginner?
Apr 23, 2017 at 11:33pm
I've been told that KDevelop is the besst for C++ , but I tend to use the command line myself.
Apr 24, 2017 at 4:58am
Thanks man & good luck programming!!!
Topic archived. No new replies allowed.