Topic: Constant expressions: is this okay?


Author: "george.ryan@gmail.com" <george.ryan@gmail.com>
Date: Thu, 18 Jun 2009 18:25:04 CST
Raw View
I'm trying to clarify N2857 7.1.4, where one of the constraints on
constexpr is:
"... where expression is a potential constant expression"

Say I have the following completely contrived functions:

int notaconstexpr( int i ) {
   // ...
   return some_computed_value;
}

constexpr int isaconstexpr( int i ) {
   return 5;
}

constexpr int foo( int i ) {
   return i < 0 ? notaconstexpr(i) : isaconstexpr(i);
}

Because the return value of foo() could potentially be a constant
expression, what would happen here?

After I typed all of that out, I realized that perhaps a recursive
computation would have been a better example, but the principle is the
same.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: CornedBee <wasti.redl@gmx.net>
Date: Sat, 20 Jun 2009 01:53:57 CST
Raw View
On Jun 19, 2:25 am, "george.r...@gmail.com" <george.r...@gmail.com>
wrote:
> I'm trying to clarify N2857 7.1.4, where one of the constraints on
> constexpr is:
> "... where expression is a potential constant expression"
>
> Say I have the following completely contrived functions:
>
> int notaconstexpr( int i ) {
>    // ...
>    return some_computed_value;
>
> }
>
> constexpr int isaconstexpr( int i ) {
>    return 5;
>
> }
>
> constexpr int foo( int i ) {
>    return i < 0 ? notaconstexpr(i) : isaconstexpr(i);
>
> }
>
> Because the return value of foo() could potentially be a constant
> expression, what would happen here?

C++0x 7.1.5p3 requires that the expression in a constexpr function be
a "potential constant expression".
5.19p6 defines this as an expression that "is a constant expression
when all occurrences of function parameters are replaced by arbitrary
constant expressions of the appropriate type."
And a constant expression has a long definition (5.19.p2), but most
importantly it says, "but subexpressions of [...] conditional
operations that are not evaluated are not considered".

By this I would conclude that a call to foo is a potential constant
expression that is a constant expression *if* i is less than zero.
However, this interpretation really hinges on the phrase "replaced by
arbitrary constant expressions". Does this mean that any replacement
must yield a constant expression? My guess is no, since then it
wouldn't be "potential".


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jerry Coffin <jerryvcoffin@yahoo.com>
Date: Sat, 20 Jun 2009 01:56:26 CST
Raw View
In article <38ced6a3-3e84-4e52-8e5f-1de4bcc9f818
@l28g2000vba.googlegroups.com>, george.ryan@gmail.com says...
> I'm trying to clarify N2857 7.1.4, where one of the constraints on
> constexpr is:
> "... where expression is a potential constant expression"
>
> Say I have the following completely contrived functions:
>
> int notaconstexpr( int i ) {
>    // ...
>    return some_computed_value;
> }
>
> constexpr int isaconstexpr( int i ) {
>    return 5;
> }
>
> constexpr int foo( int i ) {
>    return i < 0 ? notaconstexpr(i) : isaconstexpr(i);
> }
>
> Because the return value of foo() could potentially be a constant
> expression, what would happen here?

I think you've identified a problem with the current draft of the
standard. I'd suggest that the wording be added to [dcl.constexpr]/3,
bullet 4 to the effect of: "and the parameters supplied to any
functions in the potentially constant expression result in a constant
expression."

--
     Later,
     Jerry.

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]