警告:忽略模板参数上的属性..在 std::unique_ptr (-wignore 属性)的声明中

Warning: ignoring attributes on template argument... in declaration of std::unique_ptr (-Wignored-attributes)

本文关键字:属性 ptr -wignore 声明 unique std 参数 警告      更新时间:2023-10-16

使用此处解释的模式如下:

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

在 GCCv10.1.0-std=c++20中触发[-Wignored-attributes]

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++14 或更高版本中,我使用

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的错误方法。

这会将函数指针存储在编译时常量中,并可能消除该警告。