473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

What is the purpose of C++0X constexpr?

I am reading the description of "generalized constant expressions" in C
++0x here:

http://en.wikipedia.org/wiki/C%2B%2B...nt_expressions

And I don't understand the purpose of constexpr.

How are these different?

constexpr int Number = 5;
const int Number = 5;

And when would you ever want to do this:

constexpr int Number () { return 5; }
double array[Number()];

Instead of just:

const int Number = 5;
double array[Number];

Thanks,
Jason
Jun 27 '08 #1
6 7556
"ja************@gmail.com" <ja************@gmail.comwrote in
news:7c**********************************@a23g2000 hsc.googlegroups.com:
I am reading the description of "generalized constant expressions" in C
++0x here:

http://en.wikipedia.org/wiki/C%2B%2B...nt_expressions

And I don't understand the purpose of constexpr.

How are these different?

constexpr int Number = 5;
const int Number = 5;

And when would you ever want to do this:

constexpr int Number () { return 5; }
double array[Number()];

Instead of just:

const int Number = 5;
double array[Number];

Thanks,
Jason
If I recall correctly (and I probably don't), the constexpr comes from the
problem caused by certain traits classes presenting some of their constants
as functions (think numeric_limits<T>::max() for example), This means that
you can't use that value to declare an array. The constexpr feature allows
you to present your constants as a function, but yet use it anyplace a
constant can be used. I think this becomes important in metaprogramming
where a list of constants from diverse places might be put together and a
value selected at compile time to be used for dimensioning. I could be
wrong as I don't generally do that sophisticated level of template
programming.

joe
Jun 27 '08 #2
On May 2, 2:09 pm, Joe Greer <jgr...@doubletake.comwrote:
If I recall correctly (and I probably don't), the constexpr comes from the
problem caused by certain traits classes presenting some of their constants
as functions (think numeric_limits<T>::max() for example), This means that
you can't use that value to declare an array.
I see. That makes sense. I was going to say it seems like some feature
creep just to handle those rare cases, but playing around with it I
guess there is no other way to, say, declare an array of size
numeric_limits<char>::max(), to use your example.

On the other hand, I still don't understand the following example
given on the wiki page I linked to:

constexpr double forceOfGravity = 9.8;

How would that be different than:

const double forceOfGravity = 9.8;

?

Thanks,
Jason
Jun 27 '08 #3
ja************@gmail.com wrote:
On May 2, 2:09 pm, Joe Greer <jgr...@doubletake.comwrote:
>If I recall correctly (and I probably don't), the constexpr comes
from the problem caused by certain traits classes presenting some of
their constants as functions (think numeric_limits<T>::max() for
example), This means that you can't use that value to declare an
array.

I see. That makes sense. I was going to say it seems like some feature
creep just to handle those rare cases, but playing around with it I
guess there is no other way to, say, declare an array of size
numeric_limits<char>::max(), to use your example.

On the other hand, I still don't understand the following example
given on the wiki page I linked to:

constexpr double forceOfGravity = 9.8;

How would that be different than:

const double forceOfGravity = 9.8;

?
There is probably the difference in the treatment of the expressions
of a floating point type. Most of FP type expressions are not
calcualated at the compile time _unless_ (and that's what the new
feature is, I am guessing) it is declared 'constexpr'...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
ja************@gmail.com wrote:
I am reading the description of "generalized constant expressions" in
C ++0x here:

http://en.wikipedia.org/wiki/C%2B%2B...nt_expressions

And I don't understand the purpose of constexpr.

How are these different?

constexpr int Number = 5;
const int Number = 5;

And when would you ever want to do this:

constexpr int Number () { return 5; }
double array[Number()];

Instead of just:

const int Number = 5;
double array[Number];
int ArraySize()
{
return 5;
}

double Array[ArraySize()];

Illegal in C++. Even though ArraySize() returns a constant, the compiler
can't guarantee that.

constexpr int ArraySize()
{
return 5;
}

double Array[ArraySize()];

Legal in C++0x. constexpr states that the function returns a constant
expression.
--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #5
On May 2, 4:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
There is probably the difference in the treatment of the expressions
of a floating point type. Most of FP type expressions are not
calcualated at the compile time _unless_ (and that's what the new
feature is, I am guessing) it is declared 'constexpr'...
But even with integers, both of these would be valid in C++0x, right?

const int A = 5;
int array1[A];

constexpr int B = 5;
int array2[b];

Is there some difference between const and constexpr when used to
modify a variable that is assigned a literal value anyways? Or are the
above two examples identical? That's the part that I don't really
understand.

Thanks!
Jason
Jun 27 '08 #6
On 2008-05-03 04:27, ja************@gmail.com wrote:
On May 2, 4:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>There is probably the difference in the treatment of the expressions
of a floating point type. Most of FP type expressions are not
calcualated at the compile time _unless_ (and that's what the new
feature is, I am guessing) it is declared 'constexpr'...

But even with integers, both of these would be valid in C++0x, right?

const int A = 5;
int array1[A];

constexpr int B = 5;
int array2[b];

Is there some difference between const and constexpr when used to
modify a variable that is assigned a literal value anyways? Or are the
above two examples identical? That's the part that I don't really
understand.
With constexpr you can do things like this:

constexpr int square(int x)
{
return x * x;
}

float array[square(9)];

Without constexpr you can not, since the return-value of square would
have to be computed at run-time and thus the result would not be a
constant expression.

For more information and rationale see
http://www.open-std.org/jtc1/sc22/wg...2007/n2235.pdf

--
Erik Wikström
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Ioannis Vranos | last post by:
I would like to see your views on these. C++98 is already a large language since it supports 4 paradigms and each one is supported well, with optimal space and time efficiency. And this is...
35
by: GTO | last post by:
I do not believe that C# is the future of C++. I also do not believe that adding two thousand new library functions to the standard library is the future of C++. But what is the future of C++? Is...
3
by: Muhammad Farooq-i-Azam | last post by:
Hi, I am trying to define an arp structure but having problem doing so. I think I have define the correct arp structure but I find myself in a strange problem. The size of structure that I have...
7
by: Mark Odell | last post by:
I'm running two different compilers against some hairy macros and one, gcc, doesn't like my token pasting result so much. It says, " "." and "foo" does not give a valid preprocessing token ". Some...
2
by: LewGun | last post by:
at the end of last year Herb Sutter told us that "C++ 0x has been fixed", now GCC4.3 had been released, the compiler were known as "the C ++ new features' experimental unit",but it support to the...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
6
by: Dmitriy V'jukov | last post by:
On 2 Á×Ç, 20:47, "Dmitriy V'jukov" <dvyu...@gmail.comwrote: Q: Can I use Relacy Race Detector to check my algo againts other that C ++0x memory models (x86, PPC, Java, CLI)? A Yes, you...
0
by: Pooria | last post by:
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char char2; }; class Test2{
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.