Search code examples
c++templatesc++17template-argument-deduction

Template argument deduction in function signature using guide


let's say you have a class A (C++17):

template<class T>
struct A
{
    A() = default;
    A(T t) : val(t)
    {
    }
    A operator*(A a)
    {
        return A(a.val * this->val);
    }

    T val;
};

However, 99% of the time the value-type of A is going to be an int, so you use a deduction guide to reduce the verbosity:

A()->A<int>;

So that's cool, now you can define variables without the template list:

A myVar;

The problem I'm having is that it seems to break down when it comes to function signatures, for example the following requires use of a template argument list:

auto ASquared = [](A a, A b) { return a * b; };

error C2955: 'A': use of class template requires template argument list

When I wanted it to deduce that A was A<int>.

My question is: is this an inherent limitation, or am I just missing a different deduction guide that could make this syntax work?


Solution

  • The language doesn't allow this. Deduction doesn't take place within a function signature because there's nothing to deduce from.