Automatic class templates?

Is there a way for compilation to automatically output the template parameter?

template<class T> 
struct TestA 
{
    TestA(T v) {} 
};
template<class T>
void TestB(T v)
{
}
int main()
{
    TestB (5);
}

Test B works fine, however, when I change it to TestA, it will not compile with the error "using a class template requires a list of template arguments"

+3
source share
2 answers

No no. Class templates are never displayed. A regular template should have a free function make_:

template<class T> TestA<T> make_TestA(T v)
{
    return TestA<T>(v);
}

See std::pairand std::make_pairfor example.

In C ++ 0x you can do

auto someVariable = make_TestA(5);

to avoid specifying type for local variables.

+11
source

Sunlight is right, but if I can ask you a question: is this really a problem in your code. I mean:

TestA(5);

Would become

TestA<int>(5);

, , . , .

0

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


All Articles