Re: constexpr question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thursday, June 09, 2011 5:08 PM, Jonathan Wakely wrote:

The question I have is: why doesn't (or can't) the second case work? In
function 'test2' the compiler believes 'a' is a constant expression and
the

I don't think that's how it works.

In test2 the expression test1(a) is just a normal expression, calling
a function that returns an int. 'a' is not a constant expression in
the body of test2.  In the right context it can be substituted with a
constant expression, but is not actually a constant expression.

In the definition of the constexpr variable 'two' the expression
test(2) is a call to a constexpr function, so "function invocation
substitution" takes place. That substitutes the constant 2 in place of
'a' in the body of test2, producing test1(2), which is also a call to
a constexpr function, so function invocation substitution occurs
again, substituting 2 for 'a' in the body of test1, which produces
'return 2' which is a constant expression.

Similarly, in test3 'a' is not a constant expression (it's just
something which could be substituted for one in the right context) and
so it can't be used in a way which *requires* a constant expression,
such as a template parameter.

Does that help?

Hmm, I hadn't thought about it that way round.  You've given me an idea
though, so I'm going to go off and try something, to see if I can "trick"
the compiler into doing what I want!!

Cheers
Andy




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux