Compile-Time Overload Resolution

#include <cstdio>

/**
 * @brief Logs an integer value as a raw count.
 * * @param value The integer count to be logged.
 */
void logValue(int value) {
    std::printf("int=%d\n", value);
}

/**
 * @brief Logs a floating-point value as a scaled value.
 * * The compiler selects this overload when a float argument is passed.
 * * @param value The floating-point value to be logged.
 */
void logValue(float value) {
    // .2f ensures we get the "2.50" format requested
    std::printf("float=%.2f\n", value);
}

int main() {
    // Compiler matches this to logValue(int)
    logValue(10);     
    
    // Compiler matches this to logValue(float)
    logValue(2.5f);   
    
    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Expected Output

int=10 float=2.50