Search code examples
c++posixunique-ptrgcc-warning

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


Using the pattern explained here as follows:

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

triggers a [-Wignored-attributes] in 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)>
|                                                                                                ^

Why is that? Should it be ignored or is there a way to adjust the code?


Solution

  • It is saying you are ignoring the fact the function pointer doesn't throw.

    Your code has other errors, like newing a pointer that isn't cleaned up by delete.

    In or later I use

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

    You can then:

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

    but new here is probably the wrong way to create a posix_spawn_file_actions_t.

    This stores the function pointer in a compile time constant, and may get rid of that warning.