警告:在声明std::unique_ptr (-Wignored-attributes)时,忽略模板参数的属性。

问题描述 投票:1回答:1

使用解释的模式 此处 如下所示。

auto action = 
  std::unique_ptr< posix_spawn_file_actions_t, decltype(&posix_spawn_file_actions_destroy) > 
  { new posix_spawn_file_actions_t(), posix_spawn_file_actions_destroy };

触发一个 [-Wignored-attributes] 在gcc中 v10.1.0 -std=c++20:

warning: ignoring attributes on template argument ‘int (*)(posix_spawn_file_actions_t*) noexcept’
|         std::unique_ptr<posix_spawn_file_actions_t, decltype(&posix_spawn_file_actions_destroy)>
|                                                                                                ^

为什么会这样?是应该忽略它还是有办法调整代码?

c++ posix unique-ptr gcc-warning
1个回答
1
投票

这是说你忽略了函数指针不抛出的事实。

你的代码还有其他错误,比如newing一个没有被delete清理的指针。

或者以后我用

template<auto x> using kval_t=std::intergral_constant<std::decay_t<decltype(x)>,x>;
template<auto x> constexpr kval_t<x> kval={};

你可以然后。

auto action = 
  std::unique_ptr< posix_spawn_file_actions_t, kval_t<posix_spawn_file_actions_destroy> >  =
     { new posix_spawn_file_actions_t() };

但是... new 这里可能是创建一个错误的方式 posix_spawn_file_actions_t.

这将函数指针存储在编译时的常量中,并可能摆脱该警告。

© www.soinside.com 2019 - 2024. All rights reserved.