Can C++ deduce argument type from default value? [duplicate]

The type of a template parameter in a function can’t be deduced from a default argument. As shown in the example on cppreference.com: Type template parameter cannot be deduced from the type of a function default argument: template<typename T> void f(T = 5, T = 7); void g() { f(1); // OK: calls f<int>(1, 7) … Read more

template argument deduction/substitution failed, when using std::function and std::bind

To figure out the problem let separate statements: auto f = bind(&TestA::testa, &testA, _1, _2); // OK test.setCallback(f); // <<— Error is here setCallback needs to know type of T and it can’t deduce it from f, so give it a type test.setCallback<TYPE>(f); // TYPE: int, float, a class, …

Why is partial class template argument deduction impossible?

From this excellent trip report by Botond Ballo: The feature as originally proposed included a provision for partial deduction, where you explicitly specify some of the template arguments, and leave the rest to be deduced, but this was pulled over concerns that it can be very confusing in some cases: // Would have deduced tuple<int, … Read more

Why can’t T be deduced from Template::Type? [duplicate]

That is non-deducible context. That is why the template argument cannot be deduced by the compiler. Just imagine if you might have specialized TMap as follows: template <> struct TMap<SomeType> { typedef std::map <double, double> Type; }; How would the compiler deduce the type SomeType, given that TMap<SomeType>::Type is std::map<double, double>? It cannot. It’s not … Read more

What is a non-deduced context?

Deduction refers to the process of determining the type of a template parameter from a given argument. It applies to function templates, auto, and a few other cases (e.g. partial specialization). For example, consider: template <typename T> void f(std::vector<T>); Now if you say f(x), where you declared std::vector<int> x;, then T is deduced as int, … Read more

Do we still need to write the empty angle brackets when using transparent std function objects?

GCC is wrong. There is already a bug report. [dcl.type.simple]/2 says: A type-specifier of the form typenameopt nested-name-specifieropt template-name is a placeholder for a deduced class type ([dcl.type.class.deduct]). And [dcl.type.class.deduct]/2 says: A placeholder for a deduced class type can also be used in the type-specifier-seq in the new-type-id or type-id of a new-expression, as the … Read more

Template default argument loses its reference type

For foo<int>(a), ARG_T is being deduced from a, and is not taken from the default template argument. Since it’s a by value function parameter, and a is an expression of type int, it’s deduced as int. In general, default template arguments are not used when template argument deduction can discover what the argument is. But … Read more

Make a function accepting an optional to accept a non-optional?

Another version. This one doesn’t involve anything: template <typename T> void f(T&& t) { std::optional opt = std::forward<T>(t); } Class template argument deduction already does the right thing here. If t is an optional, the copy deduction candidate will be preferred and we get the same type back. Otherwise, we wrap it.

What is the partial ordering procedure in template deduction

While Xeo gave a pretty good description in the comments, I will try to give a step-by-step explanation with a working example. First of all, the first sentence from the paragraph you quoted says: For each of the templates involved there is the original function type and the transformed function type. […] Hold on, what … Read more