C++ Quiz114 of 173
Difficulty:
⬅️ Prev: 239Quiz ID: 241Next: 242 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>
#include <type_traits>

template<typename T, typename U>
void f(T, U) {
    std::cout << std::is_same_v<T, U>;
}

int main() {
    int i = 0;
    double d = 0.0;
    f(i, d);
    f<int>(i, d);
    f<double>(i, d);
}
Hint

If not all template arguments are explicitly specified, the rest are deduced.

Reveal answer

001

Explanation

f takes two template parameters, T and U, and prints out whether they are the same type. We then call f three times, each time with an int i and a double d.

In the case of f(i, d), both template arguments are deduced. Since i is an int, T is deduced to int, and since d is a double, U is deduced to double. The types are different, and 0 is printed.

For the two next calls, we explicitly specify a template argument. §[temp.arg.explicit]¶1:

Template arguments can be specified when referring to a function template specialization (...).

and §[temp.arg.explicit]¶4:

Trailing template arguments that can be deduced (...) may be omitted from the list of explicit template-arguments.

So we're allowed to explicitly specify some template arguments, and the one(s) we didn't specify are deduced as normal.

In the case of f(i, d), we specify the type of T as int. The type of U is deduced as before to double. The types are different, and 0 is printed.

In the case of f(i, d), we specify the type of T as double. The type of U is deduced as before to double. The types are the same, and 1 is printed.

Note: in the latter case, i needs to be implicitly converted to a double. Aren't implicit conversions disallowed with templates? Only if they participate in template argument deduction. §[temp.arg.explicit]¶7:

Implicit conversions (§[conv]) will be performed on a function argument to convert it to the type of the corresponding function parameter if the parameter type contains no template-parameters that participate in template argument deduction.

[Note 3: Template parameters do not participate in template argument deduction if they are explicitly specified. (...) — end note]

T did not participate in template argument deduction (since T was explicitly specified as double), so the implicit conversion is allowed.

⬅️ Prev: 239Quiz ID: 241Next: 242 ➡️