C++ Logo

std-discussion

Advanced search

Re: Specialization of std templates without quitting the current namespace

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Sat, 9 Dec 2023 14:55:41 +0300
On 12/8/23 09:46, Yongwei Wu via Std-Discussion wrote:
>
> Another issue Mike mentioned was how names should be resolved in the
> specialization: should we choose first a symbol in the "current"
> namespace, or a symbol in the "specialized" namespace? I do not hold a
> strong opinion here, but either solution seems better than having to
> exit to the global namespace.

I think, the least surprising behavior would be to resolve symbols
(apart from the template-id that is being specialized) normally,
relative to the namespace in which the specialization is being defined.
I.e.:

  namespace ns1 {
  void foo(); // 1

  template< typename T >
  struct bar
  {
    bar()
    {
      foo(); // calls #1
    }
  };
  }

  namespace ns2 {
  void foo(); // 2

  template< >
  struct ns1::bar< int >
  {
    bar()
    {
      foo(); // calls #2
    }
  };
  }

Another example:

  namespace my_ns {
  class string;

  // Specialization for my_ns::string, not std::string
  template< >
  struct std::hash< string >
  {
    std::size_t operator()(string const&) const noexcept;
  };
  }

Received on 2023-12-09 11:55:45