# C++: error: template argument deduction/substitution failed / couldn't deduce template parameter

**Code:** `template argument deduction/substitution failed` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

The compiler could not work out the template arguments from your call — the arguments disagree, the type can't be deduced, or a constraint failed.

## Meaning

Deduction looks only at function arguments. It fails when two arguments give conflicting types (`max(1, 2.5)`), when the type appears only in the return type, with braced-init-lists, or when an overload or a lambda is passed where a type is expected. Specify the argument explicitly (`f<int>(x)`) or unify the types.

## Common causes

- Conflicting argument types for the same template parameter (int and double)
- Template parameter appears only in the return type
- Braced list `{1,2}` passed to a deduced parameter
- Passing an overloaded function or generic lambda directly
- A `requires` clause or enable_if rejected the type

## Quick fix

1. Pass explicit template arguments: `std::max<double>(1, 2.5)`
2. Cast arguments to the same type
3. Read the "candidate" note to see which parameter failed

## Detailed fix by platform

### C++

1. `auto m = std::max<double>(a, b);` for mixed int/double
2. With C++17 you can often use class template argument deduction (`std::pair p{1, 2.5};`)

## Code examples

### Conflicting deduction

```cpp
#include <algorithm>

int main() {
    int a = 1; double b = 2.5;
    // auto m = std::max(a, b);          // error: no matching function; deduced conflicting types 'int' and 'double'
    auto m = std::max<double>(a, b);     // OK
    return static_cast<int>(m);
}
```

## How to diagnose

1. **Arguments** — Do all arguments of one template parameter have the same type?
2. **Return** — Does the type appear in the parameters?
3. **Constraint** — Is there a requires/enable_if?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/template-argument-deduction-failed)
