tinycodinglm / cpp /cppstd002.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.78 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How do C++ templates and concepts work?

Assistant
cpp
// Function template
template<typename T>
T max_val(T a, T b) { return a > b ? a : b; }

// Class template
template<typename T, size_t N>
class Stack {
    std::array<T, N> data;
    size_t top = 0;
public:
    void push(const T& val) { data[top++] = val; }
    T pop() { return data[--top]; }
};

// Variadic templates
template<typename... Args>
void print(Args... args) {
    (std::cout << ... << args) << '
';
}

// Concepts (C++20)
template<typename T>
concept Numeric = std::is_arithmetic_v<T>;

template<typename T>
concept Sortable = requires(T a, T b) {
    { a < b } -> std::convertible_to<bool>;
};

template<Numeric T>
T add(T a, T b) { return a + b; }

// Constraints
template<Sortable T>
void sort(std::vector<T>& v) { std::ranges::sort(v); }

// SFINAE / enable_if
template<typename T>
std::enable_if_t<std::is_integral_v<T>, T>
process(T val) { return val * 2; }

// constexpr
constexpr int factorial(int n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}
static_assert(factorial(5) == 120);