【问题标题】:Does Resharper recognize std::make_unique in a hard-code way?Resharper 是否以硬编码方式识别 std::make_unique ?
【发布时间】:2017-03-12 13:42:43
【问题描述】:

如果我编码:-

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

Resharper 会警告我应该包含B.h。 (正确)

类型“B”不完整

但是,如果我尝试模仿 std::unique_ptr&lt;T&gt; 的代码并将其放入 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>();

我不会收到任何警告。 (但由于 B 不完整,因此无法编译。)

问题

  • Resharper 是否对std::make_unique 周围的检查进行硬编码?

  • 如果不是,如何以使 Resharper 正确推荐的方式进行编码? (应包括B.h
    在实际使用中,我正在尝试创建一些自定义智能指针 + 池,
    并且我希望 Reshaper 在用户文件中正确推荐 #include

std::make_shared 也会发生这种情况。

编辑:-

正如 Igor Akhmetov 提到的那样,它是硬编码的,无论如何我能找到 Resharper 的线索吗?例如:-

//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();
}

【问题讨论】:

    标签: c++ resharper c++14 shared-ptr unique-ptr


    【解决方案1】:

    您是正确的,完整性和重载解析检查是硬编码的 std::make_uniquestd::make_shared。对于像std::make_unique 这样的函数,R++ 不能从函数的签名中推断出需要用作模板参数的类的定义。但是,std::make_uniquestd::make_unique 非常常见,因此专门为它们引入了额外的检查。

    也就是说,用作模板参数的类的定义通常需要用于转发引用的模板函数参数。在这种情况下,我们可能会考虑显示警告(但不是错误)。

    【讨论】:

    • 谢谢!虽然有点令人失望,但我可以理解实现这个功能有多难。 ..... 顺便问一下,有没有办法给 Resharper 提供线索? (我已经编辑了问题。)
    • 目前没有办法做到这一点。自然的方法是有一个特殊的属性,但看起来属性不能应用于 C++ 中的模板参数。我提交了youtrack.jetbrains.com/issue/RSCPP-19172,我们看看能不能想出一些办法。
    • 我还有一个关于 Resharper C++ 自动完成功能的问题。不确定 Resharper 能否做到。如果您能看一下,我将不胜感激。 stackoverflow.com/questions/43164856/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2013-11-20
    • 2015-12-23
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多