.
// (study.marearts.com): Compile-time Validation Pattern with consteval + static_assert
// C++20 required for consteval
#include <iostream>
// ==========================================
// EXAMPLE 1: Simple consteval function
// ==========================================
// consteval = MUST run at compile time (C++20 feature)
consteval int add(int a, int b) {
return a + b;
}
void example1() {
// This is evaluated at COMPILE TIME!
constexpr int result = add(5, 3); // Compiler computes: 5 + 3 = 8
std::cout << "(study.marearts.com) Example 1 : 5 + 3 = " << result << std::endl;
}
// ==========================================
// EXAMPLE 2: Comma operator
// ==========================================
void printMessage() {
std::cout << "Message printed!" << std::endl;
// Returns nothing (void)
}
void example2() {
// Comma operator: (A, B) evaluates A, then returns B
int x = (printMessage(), 42);
// ↓ ↓
// Calls function Returns 42
std::cout << "(study.marearts.com) Example 2: x = " << x << std::endl; // x = 42
}
// ==========================================
// EXAMPLE 3: consteval with static_assert (VALIDATION!)
// ==========================================
// Validation function - checks if number is positive
template <int N>
consteval void ValidatePositive() {
static_assert(N > 0, "ERROR: Number must be positive!");
// If N <= 0, compilation FAILS here with error message
// If N > 0, function completes
}
// Example struct that validates its template parameter
template <int Number>
struct PositiveNumber {
// This line forces validation at compile time!
// If Number is negative, compilation fails
static constexpr auto kValidation = (ValidatePositive<Number>(), 0);
// ↓ ↓
// Run checks (compile time) Return 0
int value = Number;
void print() {
std::cout << "Positive number: " << value << std::endl;
}
};
void example3() {
std::cout << "\n(study.marearts.com) Example 3: Compile-time validation" << std::endl;
// ✅ This COMPILES (5 > 0)
PositiveNumber<5> good;
good.print();
// ❌ This would FAIL to compile (uncomment to see error!)
// PositiveNumber<-3> bad;
// Error: static assertion failed: ERROR: Number must be positive!
std::cout << "✓ Validation passed at compile time!" << std::endl;
}
// ==========================================
// EXAMPLE 4: Real-world pattern (like our code!)
// ==========================================
enum class OperationType {
PASS_THROUGH,
ADD_ONE,
MULTIPLY_TWO
};
// Configuration struct (like our ConvSignature)
template <OperationType Op>
struct Config {
static constexpr OperationType operation = Op;
};
// Validation: Only allow PASS_THROUGH
template <auto Cfg>
consteval void ValidateConfig() {
static_assert(Cfg.operation == OperationType::PASS_THROUGH,
"ERROR: Simple implementation only supports PASS_THROUGH!");
}
// Simple implementation (like our ReferenceForwardFactory)
template <auto CONFIG>
struct SimpleProcessor {
// Validate at compile time!
static constexpr auto kValidation = (ValidateConfig<CONFIG>(), 0);
void process(int x) {
std::cout << "Processing " << x << " (PassThrough only)" << std::endl;
}
};
void example4() {
std::cout << "\nExample 4: Real-world validation pattern" << std::endl;
// ✅ This COMPILES (uses PASS_THROUGH)
constexpr Config<OperationType::PASS_THROUGH> good_config;
SimpleProcessor<good_config> processor;
processor.process(42);
// ❌ This would FAIL to compile (uncomment to see error!)
// constexpr Config<OperationType::ADD_ONE> bad_config;
// SimpleProcessor<bad_config> bad_processor;
// Error: static assertion failed: Simple implementation only supports PASS_THROUGH!
std::cout << "✓ Config validation passed!" << std::endl;
}
// ==========================================
// MAIN
// ==========================================
int main() {
std::cout << "=== Compile-Time Validation Study ===" << std::endl;
example1(); // consteval basics
example2(); // comma operator
example3(); // consteval + static_assert
example4(); // real-world pattern
std::cout << "\n✓ All examples passed!" << std::endl;
return 0;
}
..
KEY CONCEPTS:
1. consteval (C++20):
- Function MUST execute at compile time
- Cannot be called at runtime
2. static_assert (C++11):
- Checks condition at compile time
- If false, compilation FAILS with error message
3. Comma operator:
- (A, B) executes A, then returns B
- Used to "call void function" in member initialization
4. The Pattern:
static constexpr auto kValidation = (ValidateFunction<>(), 0);
- Forces compile-time validation
- Returns 0 if validation passes
- Compilation fails if validation fails
TO COMPILE:
g++ -std=c++20 compile_time_validation.cpp -o compile_time_validation
./compile_time_validation
=== Compile-Time Validation Study === Example 1: 5 + 3 = 8 Message printed! Example 2: x = 42 Example 3: Compile-time validation Positive number: 5 ✓ Validation passed at compile time! Example 4: Real-world validation pattern Processing 42 (PassThrough only) ✓ Config validation passed! ✓ All examples passed!
No comments:
Post a Comment