In C++, why does calling template<class T> T larger(T a, T b) as larger(1, 2.0) fail, and how do you fix it?
answer
- each argument deduces T on its own
- int from one, double from the other
- no conversions while deducing
- spell T out, or use two parameters
basics
~20 sDeduction works out T separately from each argument: int from 1, double from 2.0. The results conflict, and deduction never converts arguments to make them agree. Name T explicitly, convert an argument, or use two type parameters.
solid answer
~40 sTemplate argument deduction compares each function parameter type `P` with its argument type `A` independently. For `larger(1, 2.0)`, the first pair deduces `T = int`, the second `T = double`, and conflicting deductions make the candidate fail. Deduction matches types; it does not consider the `int`-to-`double` conversion that an ordinary function would apply. Clang reports `candidate template ignored: deduced conflicting types for parameter 'T' ('int' vs. 'double')`. Fixes: write `larger<double>(1, 2.0)` — with `T` given explicitly there is nothing to deduce, so ordinary conversions apply; cast one argument; or declare `template<class T, class U>` and compute a common return type with `std::common_type_t<T, U>`. Deduction also adjusts `A` before comparing: for a by-value parameter, top-level `const` is dropped and arrays and functions decay to pointers; for a reference parameter, the array type is kept.
code
cpp · 20 lines#include <cstdio>
#include <type_traits>
template <class T> T larger(T a, T b) { return a < b ? b : a; }
template <class T> const char* by_value(T) {
return std::is_same_v<T, const char*> ? "T = const char*" : "other";
}
template <class T> std::size_t by_ref(const T&) { return sizeof(T); }
int main() {
// larger(1, 2.0); // error: T deduced as int and as double
std::printf("%g\n", larger<double>(1, 2.5)); // T given: 1 converts to double -> 2.5
const int ci = 7;
std::printf("%d\n", larger(ci, 3)); // top-level const dropped: T = int -> 7
char name[16] = "record";
std::printf("%s\n", by_value(name)); // by value: array decays, T = char* -> other
std::printf("%s\n", by_value("abc")); // T = const char*
std::printf("%zu\n", by_ref(name)); // by reference: T = char[16] -> 16
}go deeper
Recall that the compiler fills in T from the arguments and that every argument deducing T must agree; naming T in angle brackets turns deduction off.
Explain that deduction runs per argument without conversions, list the decay and top-level const adjustments for by-value parameters, and show the explicit, cast and two-parameter fixes.
Discuss non-deduced contexts, type_identity_t to let one argument drive deduction, and how a deduction failure removes a candidate rather than producing an error on its own.
Weigh API ergonomics: a single-T signature is strict and surprising at call sites, while mixed-type signatures need a deliberate result type and may admit signed and unsigned mixes you did not intend.
## How deduction works for a call When you call a function template without naming its arguments, the compiler performs **template argument deduction**. For each function parameter whose type `P` mentions a template parameter, it compares `P` with the type `A` of the matching call argument and works out what `T` must be so the two match. Each parameter–argument pair is processed **independently**, and every pair that deduces `T` must arrive at exactly the same type. For `template <class T> T larger(T a, T b)` and the call `larger(1, 2.0)`: 1. Pair one: `P = T`, `A = int`, so `T = int`. 2. Pair two: `P = T`, `A = double`, so `T = double`. 3. The deductions conflict, so this candidate is discarded. With no other candidate, the call is an error. Clang 21 prints it plainly: ```plaintext error: no matching function for call to 'larger' note: candidate template ignored: deduced conflicting types for parameter 'T' ('int' vs. 'double') ``` ## Why no conversion rescues it For a non-template `double larger(double, double)`, the `int` would simply convert. Deduction is different: it is **pattern matching on types**, not overload resolution. It does not ask which `T` would make both arguments convertible; it reads `T` off each argument and demands agreement. The standard allows implicit conversions on an argument only when its parameter type contains no template parameter that takes part in deduction. ## The adjustments deduction does make Before comparing, the argument type is adjusted depending on the parameter's form: - **By-value parameter (`T`)**: top-level `const` and `volatile` on `A` are ignored, an array argument becomes a pointer, and a function becomes a function pointer. `const int ci; larger(ci, 3)` deduces `T = int`, and `by_value("abc")` deduces `T = const char*`. - **Reference parameter (`const T&`)**: no decay happens. Passing a `char name[16]` deduces `T = char[16]`, so `sizeof(T)` is 16. - **Braced list**: `g({1, 2, 3})` against a plain `T` deduces nothing and fails; only a `std::initializer_list<T>` or array parameter deduces from a list. - **Non-deduced contexts**: a `T` that appears only inside `typename Outer<T>::type`, or only in the return type, cannot be deduced and must be given explicitly. ## Deduction comes before overload resolution Deduction runs separately for each function template in the overload set. A template whose deduction fails simply drops out; one that succeeds contributes a specialization, such as `larger<int>`, which then competes with ordinary functions under the normal ranking rules. Two consequences follow: - A non-template `double larger(double, double)` declared beside the template rescues `larger(1, 2.0)`: the template drops out, and the ordinary function accepts the call with an `int`-to-`double` conversion. - When a template specialization and a non-template are equally good matches, the non-template wins. Explicit arguments can also be partial. For `template <class R, class T> R convert(T)`, the call `convert<long>(3)` fixes `R` and still deduces `T = int` from the argument. ## The fixes, compared | Fix | Call site | Effect | |---|---|---| | Explicit argument | `larger<double>(1, 2.0)` | `T` is fixed; `1` converts to `double` | | Convert an argument | `larger(1.0, 2.0)` or a `static_cast` | Both pairs now deduce `double` | | Two type parameters | `template <class T, class U> auto larger(T a, U b)` | Mixed types allowed; choose the result type yourself | | Block deduction on one parameter | `std::type_identity_t<T> b` (C++20) | `T` comes from `a` alone; `b` converts | The two-parameter version needs a deliberate result type — `std::common_type_t<T, U>` is the usual choice — and accepts comparisons the author may not want, such as a signed with an unsigned value. Using `std::type_identity_t` makes the second parameter a non-deduced context: `T` is deduced from the first argument only and the second converts to it. It is the C++20 standard name for a trick older code wrote by hand. ## Where this shows up in real code - `std::max(1, 2.0)` fails for the same reason; `std::max<double>(1, 2.0)` compiles. - Size arithmetic such as `std::min(v.size(), 10)` fails because `size_t` and `int` disagree. - A deduction failure is not always an error: it quietly removes that candidate from the overload set, which is the basis of the SFINAE techniques covered elsewhere.
- How does std::type_identity_t change deduction for template<class T> T larger(T a, std::type_identity_t<T> b)?`std::type_identity_t<T>` (C++20) names `T` through a nested type, which is a non-deduced context. Only the first argument deduces `T`, and the second argument then converts to that type like an argument of an ordinary function. So `larger(1, 2.0)` deduces `T = int` and converts `2.0` to `int`, returning `2` — legal, but now the first argument silently decides the type.
- Why does passing a char[16] to a const T& parameter deduce a different T than passing it by value?Deduction applies array-to-pointer decay only when the parameter is not a reference. By value, `A` becomes `char*` and `T = char*`. With `const T&`, the array binds directly, so `T = char[16]` and `sizeof(T)` is 16. Taking arrays by reference keeps the extent, which `template<class T, std::size_t N> void f(T (&)[N])` relies on to deduce `N`.
- The compiler converts 1 to double because double is the wider type.
- Deduction picks the first argument's type and converts the rest to it.
- Writing larger<double>(1, 2.0) still fails because 1 is an int.
- A by-value T parameter keeps the argument's top-level const.
- An array passed by reference decays to a pointer during deduction.
Assumes C++17 or later for the deduction rules; std::type_identity_t, one of the fixes, needs C++20.