Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[allocator.requirements.general] Consider changing Example 2 #5479

Closed
frederick-vs-ja opened this issue May 20, 2022 · 4 comments · Fixed by #5482
Closed

[allocator.requirements.general] Consider changing Example 2 #5479

frederick-vs-ja opened this issue May 20, 2022 · 4 comments · Fixed by #5482

Comments

@frederick-vs-ja
Copy link
Contributor

The example seemly contains some "legacy" styles:

  • typedef: now using is preferred;
  • operator!=: as of C++20, rewritten candidates are generally preferred;
  • globally visible equality operators: hidden friends seem preferred.

Should we change the example as following to encourage preferred styles?

template<class Tp>
struct SimpleAllocator {
  using value_type = Tp;
  SimpleAllocator(ctor args);

  template<class T> SimpleAllocator(const SimpleAllocator<T>& other);

  [[nodiscard]] Tp* allocate(std::size_t n);
  void deallocate(Tp* p, std::size_t n);

  template<class T>
  friend bool operator==(const SimpleAllocator&, const SimpleAllocator<T>&);
};
@jensmaurer
Copy link
Member

@jwakely , what do you think?

@jwakely
Copy link
Member

jwakely commented May 20, 2022

I think a normal member instead of hidden friend would be simpler for the equality op. It can even be a non-template, and rely on the converting constructor.

bool operator==(const SimpleAllocator&) const;

@jwakely
Copy link
Member

jwakely commented May 20, 2022

While we're here, should we use different template parameter names? Tp and T are unnecessarily similar. Maybe T and U.

@jwakely
Copy link
Member

jwakely commented May 20, 2022

So:

template<class T>
struct SimpleAllocator {
  using value_type = T;
  SimpleAllocator(ctor args);

  template<class U> SimpleAllocator(const SimpleAllocator<U>& other);

  [[nodiscard]] T* allocate(std::size_t n);
  void deallocate(T* p, std::size_t n);

  bool operator==(const SimpleAllocator&) const;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants