## constexpr - one of the most undervalued feature in modern C++ (1)
* constexpr was introduced in C++11
* constexpr values are know during compilation process
* Function also could be constexpr with some restriction:
* they may only have exactly one statement and this statement had to be a return statement
* not allowed to have any side effects, like if-else
* not allowed to have exception and try-catch block
* can use ternary operator
* Usually wrote recurent rather then iterative functions
* Functions behave like normal functions (not solved during compilation) when working with non-constexpr arguments
* Class/ struct could have constexpr C'tor and member functions
* In C++14 introduced:
* Less restrictions for functions:
* more then one return
* can use if-else
___
### constexpr - one of the most undervalued feature in modern C++ (2)
* In C++17 introduced:
* constexpr class string_view
* constexpr std::array
* constexpr iterator for array and std::begin, std::end functions
* constexpr lambda
* In C++20 introduced:
* constexpr STL algorithms
* constexpr std::vector and std::string and their iterators. (not supported by clang and gcc. Surprisely its supported by MSVC STL)
* Allow to use try-catch in constexpr functions
* Allow to allocate and deallocate values on heap, but need to free them before exit function
* Allow to create virtual functions
* Allow to use asm code block
___
## C++11 constexpr function
```C++
#include
constexpr bool isLower(char c) {
return c >= 'a' && c <= 'z';
}
template
constexpr size_t countLower(const T (&str)[N], size_t current, size_t counter) {
return current == N
? counter
: isLower(str[current])
? countLower(str, current + 1, counter + 1)
: countLower(str, current + 1, counter);
}
int main() {
static_assert(9 == countLower("Ala has a cat", 0, 0));
}
```
```Bash
xor eax,eax
ret
nop WORD PTR cs:[rax+rax*1+0x0]
nop DWORD PTR [rax]
```