Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class template parameter type deduction inside another class scope [duplicate]

I have some type deduction guide issue with a simple class template, that is instantiated issues in the main function scope, but fails to do that inside another class.

Here is an example:

template<class T>
struct X
{
    X(T&)
    {
    }
};

struct User
{
    X x{1}; // error: invalid use of template-name 'X' without an argument list
};

int main()
{
    int i;
    auto x = X(i); // OK
    (void)x;
}

I am kind of puzzled, why is that and how this can be fixed. Any ideas?

like image 601
Student4K Avatar asked May 06 '26 14:05

Student4K


1 Answers

Clangd gives a more meaningful error message, which would have probably made easier for you (or me, or anyone else) to search the web for an answer,

Use of class template 'X' requires template arguments;
argument deduction not allowed in non-static struct member

and the reason for this is well explained here.

like image 156
Enlico Avatar answered May 09 '26 03:05

Enlico



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!