I am trying to under the class template deduction in C++17.
I wrote a sample class template that can be constructed without specifying the template types. std::unique_ptr
can't be constructed without specifying the types.
I need help in understanding why that is the case.
Code compiled using clang 5.0
// Please don't worry about memory leaks, etc. This is sample code anyways.
template<typename T, typename deleter = std::default_delete<T>>
struct Sample
{
T* x_;
deleter func_;
Sample(T* x = nullptr, deleter func = deleter{})
: x_(x)
, func_(func)
{
}
};
auto sample = Sample(new int(10));
std::cout << *(sample.x_) << '\n';
The below code fails to compile.
auto ptr = std::unique_ptr(new int(10));