Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are constexpr functions implicitly static?

If I define a function in my program.cpp:

constexpr bool isThree(const int number)
{
  return number == 3;
}

is that any different from declaring it static?

static constexpr bool isThree(const int number)
{
  return number == 3;
}

It seems that those should be equivalent, since constexpr means the function is inline and therefore not shared among compilation units.

Are constexpr global functions implicitly static?

like image 339
Tomáš Zato - Reinstate Monica Avatar asked May 16 '19 15:05

Tomáš Zato - Reinstate Monica


2 Answers

constexpr functions are implicitly inline.

inline is a linking feature. An inline function with definitions in different compilation units is not an error; if their definitions vary, your program is ill-formed no diagnostic required, but if they have the same definition then all but one version is discarded and that version is used.

static, on a non-method function, is also a linking feature. A static definition is not shared outside of its compilation unit; the compilation unit does not 'advertise' that it has a definition for isThree.

static on a method function has nothing to do with linking. In that case, it just means that this is not implicitly passed to the function. A method with/without this it doesn't work has differences, but they are mostly unrelated to them being constexpr. Note that in at least c++14 a constexpr method that doesn't use this can still be constant evaluated. Some versions of c++ make constexpr methods implicitly const; c++17 does not.

&isThree in one compilation unit and &isThree in another can (and usually do) vary when static (barring aggressive ICF, which is a matter for a different question). When inline they may not vary.

inline functions are shared between compilation units. Their full definition is also often visible in all compilation units aware of it, so it makes compiler "inlining" (as opposed to the keyword) your code easier. static are not. constexpr functions are implicitly inline, but not implicitly static.

Note that constexpr functions can be evaluated in a runtime context sometimes. When evaluated in a compile time context, their inline vs static or linkage state really doesn't matter.

constexpr means other things as well, but you wanted to know the difference between two different constexpr declarations, and none of those meanings change.

like image 88
Yakk - Adam Nevraumont Avatar answered Nov 16 '22 01:11

Yakk - Adam Nevraumont


constexpr functions are not implicitly static. They have the same linkage as non-constexpr functions:

// external linkage
constexpr int f1(int x) { /* ... */ }

// internal linkage
static constexpr int f2(int x) { /* ... */ }

// internal linkage
namespace {
constexpr int f3(int x) { /* ... */ }
}

// no linkage
void enclosing() {
    struct S {
        constexpr int f4(int x) { /* ... */ }        
    };
}

When a constexpr function has external linkage, it has the same address in all translation units. When it has internal linkage, there is a different copy in each translation unit, and those copies have different addresses. However, I believe the result of calling a constexpr function should not depend on whether it has internal or external linkage (since constexpr functions may not contain static variables).

like image 34
Brian Bi Avatar answered Nov 16 '22 00:11

Brian Bi