C++ Programming Labs

Master compile-time programming with constexpr, type traits, and variadic templates.

Metaprogramming & Compile-Time - Module 9

Harness the power of C++ compile-time computation.

Lab 25: Constexpr Functions
Expert
Coding Challenge
Your Task: Learn constexpr - a powerful C++11/14/17 feature that enables compile-time computation, improving performance and enabling use in template arguments.

Detailed Requirements:
1. Basic constexpr function:
constexpr int square(int n) { return n * n; } // Computed at compile time! constexpr int result = square(5); // 25

2. Constexpr with conditionals (C++14+):
constexpr int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); } constexpr int fact5 = factorial(5); // 120

3. Constexpr variables:
constexpr double PI = 3.14159265358979; constexpr double circle_area(double r) { return PI * r * r; }

4. Constexpr with arrays (C++17):
constexpr int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n-1) + fibonacci(n-2); } // Use in array size int arr[fibonacci(10)]; // Size 55

Expected Output:
Square of 5: 25 Factorial of 6: 720 Circle area (r=3): 28.2743 Fibonacci(10): 55 All computed at compile time!

Requirements Checklist

Create a constexpr function
Use constexpr variable
Implement recursive constexpr
Use return statement in constexpr
Use if/conditional in constexpr
Call constexpr at compile time
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Declare: constexpr int func(int x) { return x; }
• Variable: constexpr int val = func(5);
• C++14 allows if/loops in constexpr functions
• Use for array sizes, template args, switch cases
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 26: Type Traits
Expert
Coding Challenge
Your Task: Use <type_traits> to query and manipulate types at compile time. Essential for template metaprogramming.

Detailed Requirements:
1. Include type_traits header:
#include <type_traits>

2. Check type properties:
cout << is_integral::value << endl; // 1 (true) cout << is_floating_point::value; // 1 (true) cout << is_pointer::value << endl; // 1 (true) cout << is_class::value << endl; // 1 (true)

3. Use static_assert for compile-time checks:
template void process(T value) { static_assert(is_integral::value, "T must be an integral type"); // ... process integer }

4. Type transformations:
// Remove const/reference remove_const::type x; // int remove_reference::type y; // int // Add const/pointer add_const::type z; // const int add_pointer::type p; // int*

5. Conditional types:
// Choose type based on condition conditional::type a; // int conditional::type b; // double

Expected Output:
int is integral: true double is floating: true int* is pointer: true string is class: true Types validated at compile time!

Requirements Checklist

Include type_traits header
Use is_integral type trait
Use is_floating_point trait
Use static_assert with traits
Use ::value to get result
Use remove_const or remove_reference
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Check: is_integral<T>::value
• Assert: static_assert(condition, "msg")
• Transform: remove_const<T>::type
• C++17: use is_integral_v<T> shorthand
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 27: Variadic Templates
Expert
Coding Challenge
Your Task: Master variadic templates - templates that accept any number of arguments. Used in std::tuple, std::make_unique, and more.

Detailed Requirements:
1. Parameter pack syntax:
// typename... is parameter pack template void func(Args... args) { // args... is pack expansion }

2. Count arguments with sizeof...:
template void count_args(Args... args) { cout << "Count: " << sizeof...(Args) << endl; } count_args(1, 2, 3, 4); // Count: 4

3. Print all arguments (recursive):
// Base case void print() { cout << endl; } // Recursive case template void print(T first, Rest... rest) { cout << first << " "; print(rest...); // Recurse with remaining }

4. Fold expressions (C++17):
template auto sum(Args... args) { return (args + ...); // Fold over + } cout << sum(1, 2, 3, 4); // 10

Expected Output:
Argument count: 4 Printing: 1 2 3 hello Sum: 15 Product: 120

Requirements Checklist

Use typename... parameter pack
Use sizeof... to count args
Expand pack with args...
Create recursive variadic function
Create base case for recursion
Use fold expression (+ ... or * ...)
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Pack: template<typename... Args>
• Count: sizeof...(Args)
• Expand: func(args...)
• Fold: (args + ...) or (... + args)
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below