【问题标题】:Resharper C++ and RAII - unused local variableResharper C++ 和 RAII - 未使用的局部变量
【发布时间】:2015-08-08 19:39:17
【问题描述】:

我有一个类似这样的代码:

std::unique_ptr<Object> get_raii_object()
{
    return std::make_unique<Object>(/* Many parameters that I don't want to write several times, if I remove this function altogether */ );
}

void some_code()
{
    std::unique_ptr<Object> raii_object_holder = get_raii_object();
    more_code();
}

Resharper C++ 将“raii_object_holder”标记为未使用的局部变量,尽管这是必要的。

我宁愿避免在本地或全局禁用此警告

【问题讨论】:

  • 在我看来,您可以完全避免使用 unique_ptr,因为对象只需要被构造和销毁。这会有助于重新锐化吗?
  • 查看我添加的评论。这将导致我将 ctor 参数创建复制到多个地方。
  • 到目前为止,ReSharper for C++ 中使用的解析器仍在不断发展。如果您查看他们最新的 EAP 或团队博客,您会看到即将进行巨大改进的示例。不太确定这是否已经修复,但您应该直接向 JetBrains 报告。

标签: c++ visual-studio resharper raii


【解决方案1】:

为什么不:

class ConfiguredObject : public Object
{
public:
  ConfiguredObject()
    : Object(/* Many parameters that I don't want to write several times, if I remove this function altogether */)
  {}
};

void some_code()
{
  ConfiguredObject object_holder;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-17
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2014-04-22
    相关资源
    最近更新 更多