Does constexpr compose inline?

Consider the following built-in function:

// Inline specifier version #include<iostream> #include<cstdlib> inline int f(const int x); inline int f(const int x) { return 2*x; } int main(int argc, char* argv[]) { return f(std::atoi(argv[1])); } 

and the equivalent version of constexpr:

 // Constexpr specifier version #include<iostream> #include<cstdlib> constexpr int f(const int x); constexpr int f(const int x) { return 2*x; } int main(int argc, char* argv[]) { return f(std::atoi(argv[1])); } 

My question is: the constexpr implies the inline specifier in the sense that if an inconsistent argument is passed to the constexpr function, the compiler will try to inline use the function, how would the inline specifier be placed in its declaration?

Does the C ++ 11 standard guarantee?

+87
c ++ c ++ 11 inline constexpr
Jan 18 '13 at 1:48
source share
2 answers

Yes ([dcl.constexpr], ยง7.1.5 / 2 in the C ++ 11 standard): "The constexpr functions and constexpr constructors are implicitly built-in (7.1.2)."

Note, however, that the inline specifier really does have very little (if any) effect on whether the compiler can extend the built-in function or not. However, this affects one definition rule, and from this point of view, the compiler must follow the same rules for the constexpr function as the inline function.

I should also add that no matter what constexpr means inline , the rules for constexpr functions in C ++ 11 required them to be simple enough to often be good candidates for the built-in extension (the main exception being those that are recursive). However, since then, the rules have become more and more free, so constexpr can be applied to significantly larger and more complex functions.

+125
Jan 18 '13 at 1:55
source share

constexpr does not imply inline for non-static variables (C ++ 17 built-in variables)

Although constexpr implies inline for functions, it has no such effect for non-static variables, given C ++ 17 built-in variables.

For example, if you take the minimal example that I posted at: How do built-in variables work? and delete inline , leaving only constexpr , then the variable will get several addresses, which is the main thing to avoid built-in variables.

constexpr static variables are implicitly static.

Minimal example of what constexpr implies inline for functions

As mentioned at: https://stackoverflow.com/questions/106435/ ... the main effect of inline is not to embed, but to allow several function definitions, the standard quote is: How can a C ++ header file include an implementation?

We can observe this by playing the following example:

main.cpp

 #include <cassert> #include "notmain.hpp" int main() { assert(shared_func() == notmain_func()); } 

notmain.hpp

 #ifndef NOTMAIN_HPP #define NOTMAIN_HPP inline int shared_func() { return 42; } int notmain_func(); #endif 

notmain.cpp

 #include "notmain.hpp" int notmain_func() { return shared_func(); } 

Compile and run:

 g++ -c -ggdb3 -O0 -Wall -Wextra -std=c++11 -pedantic-errors -o 'notmain.o' 'notmain.cpp' g++ -c -ggdb3 -O0 -Wall -Wextra -std=c++11 -pedantic-errors -o 'main.o' 'main.cpp' g++ -ggdb3 -O0 -Wall -Wextra -std=c++11 -pedantic-errors -o 'main.out' notmain.o main.o ./main.out 

If we remove inline from shared_func , the link will fail with:

 multiple definition of 'shared_func()' 

because the header is included in multiple .cpp files.

But if we replace inline with constexpr , then it will work again, because constexpr also implies inline .

GCC implements this by marking characters as weak in ELF object files: How can a C ++ header file include an implementation?

Tested in GCC 8.3.0.

+6
Aug 08 '19 at 7:48
source share



All Articles