Production-grade skill for C++ template programming and metaprogramming. Covers function/class templates, variadic templates, SFINAE, concepts, type traits, and compile-time computation.
Production-grade skill for C++ template programming and metaprogramming. Covers function/class templates, variadic templates, SFINAE, concepts, type traits, and compile-time computation.
sasmp_version
1.3.0
skill_version
3.0.0
bonded_agent
01-modern-cpp-expert
bond_type
PRIMARY_BOND
category
development
parameters
{"template_type":{"type":"string","required":false,"enum":["function","class","variable","alias","concept"],"description":"Type of template to work with"},"metaprogramming_level":{"type":"string","required":false,"enum":["basic","intermediate","advanced"],"default":"intermediate","description":"Complexity level of metaprogramming"},"cpp_standard":{"type":"string","required":false,"enum":["cpp11","cpp14","cpp17","cpp20","cpp23"],"default":"cpp20","description":"Target C++ standard"}}
Production-Grade Development Skill | C++ Template Metaprogramming
Master C++ template programming from basics to advanced metaprogramming techniques.
Template Basics
Function Templates
// Basic function templatetemplate<typename T>
T max(T a, T b){
return (a > b) ? a : b;
}
// Multiple template parameterstemplate<typename T, typename U>
autoadd(T a, U b) -> decltype(a + b){
return a + b;
}
// C++20: Abbreviated function templateautomultiply(auto a, auto b){
return a * b;
}
// Non-type template parametertemplate<typename T, std::size_t N>
constexpr std::size_tarray_size(T (&)[N]){
return N;
}
// Template argument deductionmax(1, 2); // T = intmax(1.0, 2.0); // T = doublemax<double>(1, 2);
// Unary right fold: (pack op ...)template<typename... Args>
autosum(Args... args){
return (args + ...); // ((a + b) + c) + d...
}
// Unary left fold: (... op pack)template<typename... Args>
autosum_left(Args... args){
return (... + args); // a + (b + (c + d...))
}
// Binary fold with inittemplate<typename... Args>
autosum_with_init(Args... args){
return (0 + ... + args); // 0 + a + b + c...
}
// Logical foldstemplate<typename... Args>
boolall(Args... args){
return (... && args); // All true
}
template<typename... Args>
boolany(Args... args){
return (... || args); // Any true
}
// Comma fold for side effectstemplate<typename F, typename... Args>
voidfor_each_arg(F f, Args&&... args){
(f(std::forward<Args>(args)), ...);
}
SFINAE and enable_if
SFINAE Basics
#include<type_traits>// Enable only for integral typestemplate<typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
safe_divide(T a, T b){
return b != 0 ? a / b : 0;
}
// C++14 styletemplate<typename T>
std::enable_if_t<std::is_floating_point_v<T>, T>
safe_divide(T a, T b) {
return b != T{0} ? a / b : std::numeric_limits<T>::quiet_NaN();
}
// Using void_t for detection idiomtemplate<typename, typename = void>
struct has_size : std::false_type {};
template<typename T>
structhas_size<T, std::void_t<decltype(std::declval<T>().size())>>
: std::true_type {};
// Usagestatic_assert(has_size<std::vector<int>>::value);
static_assert(!has_size<int>::value);
#include<concepts>// Using standard conceptstemplate<std::integral T>
T gcd(T a, T b){
while (b != 0) {
T t = b;
b = a % b;
a = t;
}
return a;
}
// Common standard conceptstemplate<std::floating_point T>
T sqrt_approx(T x);
template<std::copyable T>
voidprocess(T value);
template<std::invocable<int> F>
voidapply(F&& func);
template<std::ranges::range R>
voiditerate(R&& range);