Search code examples
c++resharperc++14shared-ptrunique-ptr

Does Resharper recognize std::make_unique in a hard-code way?


If I code :-

class B;  //some B.h has definition of B
std::make_unique<B>();  

Resharper will warn me that I should include B.h. (correct)

Type 'B' is incomplete

However, if I try to mimic code of std::unique_ptr<T> and put it inside Test.h:-

Test.h

//inside class Test
template<class _Ty,
class... _Types> inline
typename std::enable_if<!std::is_array<_Ty>::value,
    std::unique_ptr<_Ty> >::type test2(_Types&&... _Args)
{   // make a unique_ptr
return (std::unique_ptr<_Ty>(new _Ty( std::forward<_Types>(_Args)...)));
}

Test.cpp

#include "Test.h"
//inside some function
test2<B>();

I will not get any warning. (but it is uncompilable because B is incomplete.)

Question

  • Does Resharper hardcode the checking around std::make_unique?

  • If not, how to code in a way that makes Resharper correctly recommend? (should include B.h)
    In the real usage, I am trying to create some custom smart pointer + pool,
    and I want Reshaper to correctly recommend #include in the user's file.

It also occurs to std::make_shared.

Edit:-

As Igor Akhmetov mentioned that it was hardcoded, is there anyway I can clue Resharper? e.g. :-

//Hey, Resharper, the user must include full definition of "T".
//    Resharper, forward declaration is not enough for "T".
template<class T> T* f(){
    return new T();
}

Solution

  • You are correct, completeness and overload resolution checks are hardcoded for std::make_unique and std::make_shared. For functions like std::make_unique, R++ can't deduce from function's signature that definition of the class used as a template argument is needed. However, std::make_unique and std::make_unique are so common, that additional checks were introduced specifically for them.

    That said, definitions of classes used as template arguments are usually required for template function parameters that are forwarding references. We might consider showing a warning (but not an error) in this case.