WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Expert · question 70 of 100

How can you use C++ metaprogramming to generate code at compile-time? Provide an example.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

C++ metaprogramming is a technique that involves writing C++ code that is executed by the compiler during the compilation process. The goal is to generate code at compile-time, rather than runtime, to improve performance and reduce code size. There are several techniques for implementing metaprogramming in C++, including template metaprogramming, constexpr functions, and variadic templates.

One common use case for metaprogramming is to generate code based on a set of predefined rules or conditions. For example, suppose we want to define a set of functions that operate on integer values. We could write a series of functions that take integer arguments and perform specific operations, such as adding, subtracting, multiplying, and dividing:

    int add(int a, int b) {
        return a + b;
    }
    
    int subtract(int a, int b) {
        return a - b;
    }
    
    int multiply(int a, int b) {
        return a * b;
    }
    
    int divide(int a, int b) {
        return a / b;
    }

However, this approach can quickly become cumbersome and repetitive, especially if we want to define additional operations. A better approach is to use metaprogramming to generate these functions based on a set of rules. One way to do this is to use template metaprogramming, which involves defining a template class that recursively generates code based on a set of rules.

For example, we could define a template class called "Operation" that takes two template parameters, the operation type and the operand type. The operation type is a type trait that defines the operation to be performed, while the operand type is the type of the operands. We can then define a series of specialized templates for each type of operation:

    template <typename T>
    struct Add {
        static T apply(T a, T b) { return a + b; }
    };
    
    template <typename T>
    struct Subtract {
        static T apply(T a, T b) { return a - b; }
    };
    
    template <typename T>
    struct Multiply {
        static T apply(T a, T b) { return a * b; }
    };
    
    template <typename T>
    struct Divide {
        static T apply(T a, T b) { return a / b; }
    };

We can then define a template class called "Calculator" that takes three template parameters, the operation type, the operand type, and the number of operands. The Calculator class uses template specialization to recursively generate code for the specified number of operands:

    template <template <typename> class Operation, typename T, int N>
    struct Calculator {
        static T apply(const std::array<T, N>& operands) {
            return Operation<T>::apply(operands[N-1], Calculator<Operation, T, N-1>::apply(operands));
        }
    };
    
    template <template <typename> class Operation, typename T>
    struct Calculator<Operation, T, 1> {
        static T apply(const std::array<T, 1>& operands) {
            return operands[0];
        }
    };

We can then use these template classes to generate code at compile-time for any number of operands and any type of operation:

    int main() {
        std::array<int, 3> operands = {2, 3, 4};
        int result = Calculator<Add, int, 3>::apply(operands);
        std::cout << "Addition result: " << result << std::endl;
        result = Calculator<Subtract, int, 3>::apply(operands);
        std::cout << "Subtraction result: " << result << std::endl;
        result = Calculator<Multiply, int, 3>::apply(operands);
    }
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics