I really can't find any use of it. My first idea was that I could use it to implement 'Design by Contract' without using macros like this:
struct S
{
S(constexpr int i) : S(i) { static_assert( i < 9, "i must be < 9" ); }
S(int i); //external defintion
char *pSomeMemory;
};
But this wouldn't compile. I thought we could also use it to reference same-variable without the need of additional memory to be created when we want to avoid the get/setters in order to make instances to one member from users to be read-only:
class S
{
private:
int _i;
public:
const int & constexpr i = _i;
};
But none of the above actually compiled. Can someone give me some insight why this keyword was being introduced?
Understanding constexpr Specifier in C++ constexpr is a feature added in C++ 11. The main idea is a performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized by the developer, it is run multiple times by users.
Yes. I believe putting such const ness is always a good practice wherever you can. For example in your class if a given method is not modifying any member then you always tend to put a const keyword in the end.
#define directives create macro substitution, while constexpr variables are special type of variables. They literally have nothing in common beside the fact that before constexpr (or even const ) variables were available, macros were sometimes used when currently constexpr variable can be used.
const applies for variables, and prevents them from being modified in your code. constexpr tells the compiler that this expression results in a compile time constant value, so it can be used in places like array lengths, assigning to const variables, etc.
The goal of constexpr
depends on the context:
For objects it indicates that the object is immutable and shall be constructed at compile-time. Aside from moving operations to compile-time rather than doing them at run-time creating constexpr
objects has the added advantage that they are initialize before any threads are created. As a result, their access never needs any synchronization. An example of declaring an object as constexpr
would look like this:
constexpr T value{args};
Obviously, for that to work, args
need to be constant expressions.
For functions it indicates that calling the function can result in a constant expression. Whether the result of constexpr
function call results in a constant expression depends on the arguments and the definition of the function. The immediate implication is that the function has to be inline
(it will implicitly be made so). In addition, there are constraints on what can be done within such a function. For C++11 the function can have only one statement which, for non-constructors, has to be a return
-statement. This restriction got relaxed in C++14. For example, the following is a definition of a constexpr
function:
constexpr int square(int value) { return value * value; }
When creating constexpr
object of non-built-in types the respective types will need a constexpr
constructor: the generated default constructor won't work. Obviously, a constexpr
constructor will need to initialize all members. A constexpr
constructor could look like this:
struct example {
int value;
constexpr example(int value): value(value) {}
};
int main() {
constexpr example size{17};
int array[size.value] = {};
}
The created constexpr
values can be used everywhere a constant expression is expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With