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

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

使用模式解释 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 v10.1.0 中触发 [-Wignored-attributes] -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)>
|                                                                                                ^

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

意思是你忽略了函数指针不抛出异常的事实。

您的代码有其他错误,例如新建一个未被删除清除的指针。

或以后我使用

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

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