Process Sensor Reading

#include <iostream>
#include <type_traits>
#include <cmath>
using namespace std;
// Write your two overloaded process() functions here
template<typename T>
void process(const T& x){
    cout << "readonly";
}
// modifying version
template<typename T>
void process(T& x){
    if constexpr (std::is_integral<T>::value){
        x = x * 2;
    }
}
int main() {
    int x;
    std::cin >> x;

    if (x < 0) {
        process(static_cast<const int&>(x));   // call read-only overload
    } else {
        process(x);                            // call modifying overload
        std::cout << x;
    }

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

10