libraries I created for this code
• Discuss some techniques I found building compile-time libraries
## constexpr in Brief
• Specifies a variable or function CAN appear in a constant expression
• Constant expressions expressions can be evaluated at compile-time
• eg:
■ Array size
☐ non-type template parameter
## constexpr Variable
• Must be a Literal Type
☐ scalar (int, char, etc)
array of literals
■ struct/class/union Implies const
constexpr int Base{0b0010'1000};
constexpr std::array Values{1, 2, 3};
int main() {
// Base += 1; // ERROR
return Base + Values[1];
}
# constexpr Function
reporting can be used with compile-time error detection
void foo() {
constexpr auto error = detect_error();
if constexpr (error) {
report_error(error);
}
}
## Error Reporting – static\_assert message must be a string literal which limits the information that can be provided
void foo() {
constexpr auto error = detect_error();
static_assert(!error, "error message");
## Error Message
foo() {
constexpr auto error = std::optional(custom_error{});
if constexpr (error) {
report_error<*error>();
}
};
## Error Message
template
constexpr auto report_error()
Readings 13
Chapter 02: Language Usability Enhancements 13
2.1 Constants 13
nullptr 13
constexpr 15
2.2 Variables and initialization 17
if-switch 17
Initialize list 18
Structured binding
auto 21
decltype 22
tail type inference 23
decltype(auto) 24
2.4 Control flow 25
if constexpr 25
Range-based for loop 26
2.5 Templates 26
Extern templates ..... 26
The “>” ..... 27 the equality of the two types. We will discuss them in detail later in the decltype section.
## constexpr
C++ itself already has the concept of constant expressions, such as $ 1+2 $ , $ 3*4 $ . Such
reports and contributions from users, I learned about:
• Template Meta Programming
• Lambdas
constexpr
## ChaiScript
ChaiScript had a moderate amount of success, and through its development and via reports and contributions from users, I learned about:
• Template Meta Programming
• Lambdas
constexpr
• Static Analysis
## ChaiScript
ChaiScript had a moderate amount of success, and through its development reports and contributions from users, I learned about:
• Template Meta Programming
• Lambdas
constexpr
• Static Analysis
• Compiler Warning Options
## ChaiScript
## ChaiScript had a moderate amount
Concept|X is Concept|?|
|X is Template|X is Template|?|
template< typename C, typename X>
constexpr auto is( X const& ) -> bool;
Expected
Actual type
x is Type
template
auto auto is(C const&) -> bool {
return true;
}
template
constexpr auto is(X const&) -> bool
{
return false;
}
x is Type
template auto is(C const&) -> bool {
return true;
}
template
constexpr auto is(X const&) -> bool
{
: In function 'int main()':
:118:14: