Template argument deduction when types are not in function prototype

Will T

According to https://en.cppreference.com/w/cpp/language/template_specialization, "When specializing a function template, its template arguments can be omitted if template argument deduction can provide them from the function arguments".

Is it every possible to provide an explicit type for specialisation when the type cannot be deducted?

Consider this basic example:

#include <iostream>

using namespace std;

template<typename T>
void print(int i) {
    T t = static_cast<T>(i) * static_cast<T>(1.5);
    cout << t << endl;
}

int main()
{
    print<double>(5);
    print<int>(5);

    return 0;
}

What if we wanted to provide a specialisation for a type, like int that does something different, even though int doesn't appear in the function prototype? e.g. I'm looking for a way to do something like this:

template<T = int>
void print(int i) {
    cout << i << endl;
}
463035818_is_not_a_number

A specialization looks like this:

template<>
void print<int>(int i) {
    cout << i << endl;
}

The sentence you read refers to the case

tempalte <typename T> void foo(T);

Where a specialization can be declared via a shorter

template <> void foo(int);

as an alternativ to the usual

tempalte <> void foo<int>(int);

The latter is ok in either case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Argument deduction inside function template

std::function template argument deduction

template argument deduction/substitution failed, when boost::binding a templated function

Template argument deduction for an argument of a function type

c++ template argument deduction in function prototype. possible workaround in this case?

Dependent Types: Template argument deduction failed

template argument deduction of return type in function template

Deduction of Return / Argument types from function

function templates, partial application and template argument deduction

Template function argument deduction with an implicit conversion

Template argument deduction in function signature using guide

function template deduction using raw pointer as argument

`std::function` template argument deduction/substitution failed

variadic template argument deduction for function pointer arguments

Template function deduction fail on std::conditional argument

Template argument deduction for inherited member function

How does template argument deduction work when an overloaded function is involved as an argument?

passing function template prototype as argument

Template argument deduction when the function returns a type composed from the template type and another

Function template argument deduction with variadic class template as function call parameter

Explanation of the C++ template function argument deduction when matching `T const &&t` against `int const *`

Template argument deduction/substitution failed, when using typename argument

Function template argument deduction (class vs funtion template)

Nested template argument deduction

Understanding template argument deduction

Template argument deduction order

Confused by template argument deduction

Template Argument Deduction issues

concepts template argument deduction