Implicit type conversion with templated function parameters

c++ template function specialization
c++ function as template parameter
template argument deduction
non-type template parameter
c template member function
c template return type
template template parameter
template argument deduction/substitution failed

If I have a simple function expecting a type like:

class X
{
    public:
        X( int ){}
};

void fx( X  ) {}



fx( 1 ); // implicit converted to X(int); // fine!

If I try the same for templated types, it will not work.

template <typename T>
class Y
{
    public:
        Y( T ){};
};

template <typename T>
void fy( Y<T> );

fy( 2 ); // Y<int> expected, but it fails

Is there any trick to force the conversion?

It is needed to do it implicit, a direct access on fy is not what is wanted. I know that I can force all templates with specifying the template parameters ;)

Implicit conversions won't be considered in template argument deduction; the template parameter T just can't be deduced.

Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.

You can write a helper function template.

template <typename T>
void helper(T&& t) {
    fy<std::decay_t<T>>(std::forward<T>(t)); // specify the template argument explicitly
}

then

helper(2); // T will be deduced as int

Template argument deduction, . A pointer or reference to a class member function. Implicit conversions are performed whenever an expression of some type T1 is used in context that does not accept that type, but accepts some other type T2; in particular: when the expression is used as the argument when calling a function that is declared with T2 as parameter;

You cannot have implicit conversion and template deduction in the argument. Another way to break it up:

template <typename T>
void fy( T x ) {
    Y<T> y = x;
    //Use y
    return;
}

Of course, depending on fy you may be able to use x directly as T, and convert implicitly on the fly in the function.

Implicit Type Conversions | Programming with OpenCL C, Why must the template parameter appear in the parameter list of the template function? But, in the case where you require implicit type conversions on friend functions of template classes and can guarantee that ADL will succeed in your function calls, approach #3 will work, and in the worst case it is no more difficult to use than approach #2. Conclusion. C++ has three approaches for implementing friend functions of template classes.

Template argument deduction does not take any implicit conversions into account.

You can manually specify the desired instantiation:

fy<int>(2);

Using template type deduction with C++17, you can also go with

fy(Y(2));

and with pre-C++17

fy(Y<int>(2));

13.4, Template arguments of a function template may be specified) are subject to implicit conversions to the type of the  Implicit conversions are attempted when: An argument supplied to a function does not have the same type as the matching parameter. The value returned from a function does not have the same type as the function return type. An initializer expression does not have the same type as the object it is initializing.

Computer Programming/Type conversion, Single-argument constructors: allow implicit conversion from a particular type On a function call, C++ allows one implicit conversion to happen for each argument There exist two main syntaxes for generic type-casting: functional and c-like:  in the context of a call to a user-defined conversion function, the return types of the conversion function templates are used in other contexts, the function template type is used Each type from the list above from the parameter template is deduced.

Templates, As we saw in approach #2, C++ won't implicitly convert types for template function parameters, even if such a conversion exists. But in the case where we are  User-defined conversion operators (C# reference) A user-defined type can define a custom implicit or explicit conversion from or to another type. Implicit conversions don't require special syntax to be invoked and can occur in a variety of situations, for example, in assignments and methods invocations.

Function template, Strong types and implicit conversions, doesn't this sound like It seems natural to template this class on the destination type of the conversion. that we'll pass copies of the underlying value as arguments to the function. Hence, it will in turn be applied to a matching sequence of implicit arguments. The end effect is that references to implicit functions get applied to implicit arguments in the same way as references to implicit methods. The second rule is the dual of the first. If the expected type of an expression t is an implicit function type

Comments
  • @Ron: Sorry to be non precise enough: For me it was important to get it implicit. Manually defining the template parms did not help in my use case.
  • Y<const int&>, Y<int>, Y<int&&> would be viable. How do you expect compiler to decide ?
  • OK, so the answer is simply: No! :-) because I though about writing a helper which needs implicit conversion, writing a helper which did it explicit is the opposite of my basic idea. My idea was simply based on "implicit" conversion so it simply was not a possible way. Thanks!
  • @songyuanyao: You probably want std::decay_t<T> to handle (const) references.
  • If you already have the code for fy(Y<T>) you could just write template <class T> void fy(T x) { fy(Y<T>(x)); } as overload wrapper.
  • Sorry to be non precise enough: For me it was important to get it implicit. Manually defining the template parms did not help in my use case.