c++ - 警告:在std::unique_ptr的声明中忽略模板参数上的属性(-Wignored-attributes)

标签 c++ posix unique-ptr gcc-warning

使用here解释的模式如下:

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 };

触发gcc [-Wignored-attributes] 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)>
|                                                                                                ^

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

最佳答案

就是说您忽略了函数指针不会抛出的事实。

您的代码还有其他错误,例如,更新了未被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的错误方法。

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

关于c++ - 警告:在std::unique_ptr的声明中忽略模板参数上的属性(-Wignored-attributes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62412126/

相关文章:

c++ - SVN repo http ://server/. ../和 svn ://server/. ./有什么区别

c++ - long * tempArray[10]; 是什么意思?做?

c - posix 线程中的线程取消

posix - 我如何确定是否是 POSIX 应用程序。在系统启动后 5 分钟内运行?

c++ - 将 unique_ptr<std::vector> 与 C 风格数组结合使用

c++ - 循环单链表,添加新节点并显示

c++ - 连续发送帧时提升asio async_receive_from()丢失udp帧数据检索

c - 为什么未命名的信号量在共享内存中使用时不会改变?

c++ - 我是否总是必须使用 unique_ptr 来表达所有权?

c++ - 使用模板向上转换 unique_ptr