All submissions

Threshold Default Template Arguments

#include <iostream>
using namespace std;

template<typename T = int>
T thresholdOrDefault(T v, T def = T())
{
    // Example logic: if v is less than 0, return def; otherwise return v
    return (v < 0) ? def : v;
}

int main()
{
    int v, def;
    if (cin.peek() == '-')  // Check if first character is '-'
    {
        cin >> v;
        char next = cin.peek();  // Peek at next character

        if (next == ' ')  // If there's a space, assume another value follows
        {
            cin >> def;
            cout << thresholdOrDefault(v, def) << endl;
        }
        else
        {
            cout << thresholdOrDefault(v) << endl;
        }
    }
    else
    {
        cin >> v;
        cout << thresholdOrDefault(v) << endl;
    }
}
Loading...

Input

5

Expected Output

5