Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 416
Nicolas Holthaus Avatar asked Mar 26 '26 08:03

Nicolas Holthaus


1 Answers

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

like image 72
Brian Bi Avatar answered Mar 28 '26 03:03

Brian Bi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!