使用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清除的指针。
在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
的错误方法。这会将函数指针存储在编译时常量中,并且可以摆脱该警告。
关于c++ - 警告:在std::unique_ptr的声明中忽略模板参数上的属性(-Wignored-attributes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62412126/