Why overload one parameter template function besides variadic template?
Asked Answered
M

0

8

I read some code from <any> in gcc 10.2.0 source

    // Manage in-place contained object.
    template<typename _Tp>
      struct _Manager_internal
      {
    static void
    _S_manage(_Op __which, const any* __anyp, _Arg* __arg);

    template<typename _Up>
      static void
      _S_create(_Storage& __storage, _Up&& __value)
      {
        void* __addr = &__storage._M_buffer;
        ::new (__addr) _Tp(std::forward<_Up>(__value));
      }

    template<typename... _Args>
      static void
      _S_create(_Storage& __storage, _Args&&... __args)
      {
        void* __addr = &__storage._M_buffer;
        ::new (__addr) _Tp(std::forward<_Args>(__args)...);
      }
      };

And I can't understand why there are two _S_create, the second is superset of the first, isn't it? Can we omit the first one?

Mayman answered 1/8, 2020 at 9:18 Comment(7)
I am with you. I can't see any reason for the specialisation of the single argument version... Maybe you simply write a bug report as this can remove unnecessary code from the libs. Give it a try!Strohben
@Strohben I didn't do it before, should i send email to [email protected] ?Mayman
gcc.gnu.org/bugzillaCondonation
Have you checked that a default template argument is not provided for the parameter Up in any header file? In this case the secong would be used if the Up is not deducibleInkerman
@Condonation Thank you, I committed gcc.gnu.org/bugzilla/show_bug.cgi?id=96413Mayman
@Inkerman sorry, i can't understand, can you show me code?Mayman
@godizzy, Sorry I had not seen that _S_create is a member of a class template, in this specific case, default argument cannot be added. Otherwise the trick I was speaking about consist in giving _Up a default argument, for exemple typename _Up = initializer_list<int> in order that the call _S_create(storage, {1,2,3}) is well formed.Inkerman

© 2022 - 2024 — McMap. All rights reserved.