Saturday, September 30, 2023

constexpr

Overview
Consider following code.  
struct data
{
    const char *name;
    short yob;
    const char *description;
};

auto ntop10 = 10;
array<data, ntop10> top10
{{
    {"Marilyn Monroe",1926 ,"American actress, singer, model"},
    {"Abraham Lincoln",1809 ,"US President during American civil war"},
    {"Nelson Mandela",1918 ,"South African President anti-apartheid campaigner"},
    {"Queen Elizabeth II",1926 ,"British monarch since 1954"},
    {"John F. Kennedy",1917 ,"US President 1961 – 1963"},
    {"Martin Luther King",1929 ,"American civil rights campaigner"},
    {"Winston Churchill",1874 ,"British Prime Minister during WWII"},
    {"Donald Trump",1946 ,"Businessman, US President."},
    {"Bill Gates",1955 ,"American businessman, founder of Microsoft"},
    {"Muhammad Ali",1942 ,"American Boxer and civil rights campaigner"}
}};

double vyasa (double trijya) {return 2.0 * trijya ;}
double kshetra (double trijya) {return trijya * 3.14 * trijya;}
double gems[] {kshetra(10),diaherea(20),kshetra(30),kshetra(40)};
double dias[] {vyasa (10),vyasa (20),vyasa (30),vyasa (40)};
Before constexpr all this was executed at runtime. With constexpr now all these is executed at compile time. Now it's possible that 0 to 100% of code of any existing projects can be optimized during compile time. This is depicted in the example 6
Some examples of constexpr are fixed font generation, object database loading, generating lookup tables etc.

Details
The keyword constexpr means constant expression. Like const, it can be applied to variables: A compiler error is raised when any code attempts to modify the value. Unlike constconstexpr can also be applied to functions and class constructors. constexpr indicates that the value, or return value, is constant and, where possible, is computed at compile time.
constexpr integral value can be used wherever a const integer is required, such as in template arguments and array declarations.

constexpr variables
The primary difference between const and constexpr variables is that the initialization of a const variable can be deferred until run time. A constexpr variable must be initialized at compile time. All constexpr variables are const.
A variable can be declared with constexpr , when it has a literal type and is initialized. If the initialization is performed by a constructor, the constructor must be declared as constexpr.
A reference may be declared as constexpr when both these conditions are met: The referenced object is initialized by a constant expression, and any implicit conversions invoked during initialization are also constant expressions.
constexpr float x = 42.0;
constexpr float y{108};
constexpr float float_array[std::numeric_limits<short>::max()];

It's possible to integral static variables in a class such as int but not float. In such cases constexpr can be used as shown below.
struct s
{
    static const float f=9.0;       //error
    static constexpr float f2=9.0;  //ok
    static const int i=9;           //ok
};

constexpr functions
In a constexpr function, its return value is computable at compile time. The consuming code can use the return value at compile time to initialize a constexpr variable. When its arguments are constexpr values, a constexpr function produces a compile-time constant. When called with regular arguments, or when its value isn't required at compile time, it produces a value at run time like a regular function. Therefore the same function can be reused.
constexpr int factorial(int n)
{
    return n <= 1 ? 1 : (n * factorial(n - 1));
}
//compile time example
static_assert((6 == factorial(3)),"compile time");
This is depicted in the example 7

constexpr constructor
A constructor that is declared with a constexpr  specifier is a constexpr constructor.  It's also bound to restrictions as in case of  constexpr function. 
struct Rectangle
{
    int h, w;
    // constexpr constructor
    constexpr Rectangle(int h, int w) : h(h), w(w) {}
};
//example
constexpr Rectangle r{ 10, 20 };
This is depicted in the example 8


No comments:

Post a Comment