constexpr vas inline??

Hi,

Yes, the question sounds silly, I know... But, nevertheless I need to ask it: because:

say we have

1
2
3
4
constexpr double square( double x )
{
    return x*x;
}


As constexpr means a compile time function, and we know if it is invoked with a constant, the result is a constant known at compile time -- but does that mean it executes the multiplication during compilation?
If it does, couldn't we say constexpr functions are "super-inlines"?
And, when invoked with a variable, what do we win compared to an ordinary inline? I mean, the code x*x is inlined isn't it?

Well, maybe it wasn't that dumb of a question???


Thanks
Juan
As constexpr means a compile time function

constexpr means it may be evaluated given the right conditions at the time of compilation.


and we know if it is invoked with a constant, the result is a constant known at compile time -- but does that mean it executes the multiplication during compilation?

It would be hard to know the result of a calculation if a calculation wasn't done.


If it does, couldn't we say constexpr functions are "super-inlines"?

You can say anything you like.


And, when invoked with a variable, what do we win compared to an ordinary inline? I mean, the code x*x is inlined isn't it?

It may or may not be. The inline keyword is more about linkage than inlining.
http://stackoverflow.com/a/157929



Seems to me you are not taking my question seriously. Specifically:

couldn't we say constexpr functions are "super-inlines"?


What I meant was, if parameters are not known at compile time, what difference does it have from an inline version? And yes, I understand your point on inline linkage. If the code, x*x in this case, is not inlined, then in what respects does the constexpr function differ from an inline function and a regular function?

Thanks,
Juan
Seems to me you are not taking my question seriously. Specifically:

I have a hard time taking questions seriously where people are asking about the validity of made up terminology.


What I meant was, if parameters are not known at compile time, what difference does it have from an inline version?

Functions with the specifier constexpr are implicitly inline.


If the code, x*x in this case, is not inlined, then in what respects does the constexpr function differ from an inline function and a regular function?

It differs from a regular function in the same way inline functions do.
Because there has been some miscommunication, let me summarise my whole question this way:

Multiplication as in calling the function:

1
2
3
4
double square( double x )
{
    return x*x;
}


always executes at run time. Yet, when the function is declared
constexpr
, does multiplication occur at compile time if and only if its arguments are known at compile time, as in:

1
2
3
4
5
6
7
8
constexpr double square( double x )
{
    return x*x;
}

void useSquare() {
    auto sq = square(12.5);
}


?

Thanks!
Juan
Last edited on
Hi,

I might attempt to answer this :+)

In this link, a constexpr function must satisfy the requirements, otherwise it is an error?
http://en.cppreference.com/w/cpp/language/constexpr

In particular, not one of these 23 things:
http://en.cppreference.com/w/cpp/language/constant_expression#Core_constant_expressions

Jaun wrote:
, does multiplication occur at compile time if and only if its arguments are known at compile time, as in:


So if the 12.5 was a non compile time non const variable, that would be an error.

cppreference wrote:
The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time.


Does this help?

Topic archived. No new replies allowed.