Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template deduction failed on the heap but works on the stack

The fact new MethodPtr(ptr, func) fails the deduction is a compiler bug indeed. According to [dcl.type.class.deduct]/2:

A placeholder for a deduced class type can also be used in the type-specifier-seq in the new-type-id or type-id of a new-expression, or as the simple-type-specifier in an explicit type conversion (functional notation) ([expr.type.conv]). A placeholder for a deduced class type shall not appear in any other context.

As you can see, an explicit yes for a new expression, and a blanket ban for anything not explicitly allowed. So make_unique can't be given a placeholder.

Unless you can migrate to a GCC version where this has been fixed (or you just need to use make_unique), you can't avoid decltype. Try to introduce a type alias to mitigate the inconvenience.


In latest gcc and clang it works - this new MethodPtr(ptr, func). So for gcc7.2 - it is a bug.

For unique_ptr(new MethodPtr(ptr, func)) - it cannot work - because in C++ - at this level, having MethodPtr* - we cannot distinguish between unique_ptr<MethodPtr[]> and unique_ptr<MethodPtr> - so it cannot be deduced.


Look here. Passing simply MethodPtr (class template) as a non-template type parameter of function template (std::make_unique) was never allowed, and class template argument deduction didn't change that.

new MethodPtr{ptr, func}; works, by looking at the reference, but I can't say the reason why should that be different to new MethodPtr(ptr, func);