How to call a template ctor template class?

template<typename T>
struct A
{
    template<typename U>
    A() {}

    template<typename U>
    static void f() {}
};

int main()
{
    A<int>::f<int>(); // ok
    auto a = A<int><double>(); // error C2062: type 'double' unexpected
}

The problem is obvious in the code.

My question is:

How to call a template ctor template class?

+4
source share
2 answers

You cannot directly call a class constructor. If you cannot deduce the arguments of the constructor template from the call, then this particular constructor cannot be called.

What you can do is create some kind of wrapper that can be used for zero-load output:

template <typename T>
struct type_wrapper { };

template<typename T>
struct A
{
    template<typename U>
    A(type_wrapper<U>) {}
};

int main()
{
    auto a = A<int>(type_wrapper<double>{});
}

live example in wandbox

+5
source

How to call a template ctor template class?

Unfortunately this is not possible; You cannot explicitly specify template arguments for constructor templates.

§17.5.2/5 [temp.mem]

( )

[. , - - , . - ]

+3

Source: https://habr.com/ru/post/1676493/


All Articles