Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't unique_ptr's template arguments be deduced?

Tags:

When you have class template argument deduction available from C++17, why can't you deduce the template arguments of std::unique_ptr? For example, this gives me an error:

std::unique_ptr smp(new D); 

That says "Argument list of class template is missing".

Shouldn't the template arguments (at least the pointer type) be deducable?

See this:

any declaration that specifies initialization of a variable and variable template

like image 526
Ukula Udan Avatar asked Jun 29 '18 21:06

Ukula Udan


People also ask

What is template argument deduction?

Template argument deduction is used in declarations of functions, when deducing the meaning of the auto specifier in the function's return type, from the return statement.

What is a deduction Guide?

Template deduction guides are patterns associated with a template class that tell the compiler how to translate a set of constructor arguments (and their types) into template parameters for the class. The simplest example is that of std::vector and its constructor that takes an iterator pair.

What is type deduction c++?

Type inference or deduction refers to the automatic detection of the data type of an expression in a programming language. It is a feature present in some strongly statically typed languages. In C++, the auto keyword(added in C++ 11) is used for automatic type deduction.


1 Answers

Lets look at new int and new int[10]. Both of those return an int*. There is no way to tell if you should have unique_ptr<int> or unique_ptr<int[]>. That right there is enough not to provide any sort of deduction guide.

like image 106
NathanOliver Avatar answered Sep 20 '22 18:09

NathanOliver