Add a pmr alias for std::stacktrace

Document #: P2301R1
Date: 2021-06-14
Project: Programming Language C++
Audience: LEWG, LWG
Reply-to: Steve Downey
<, >

Abstract: This paper proposes to add an alias in the pmr namespace defaulting the allocator used by the std::basic_stacktrace template to pmr::allocator. No changes to the api of std::stacktrace are necessary.

1 Changes

1.1 Changes since R0

Removed unneeded “std::” in the pmr alias.

  namespace pmr {
    using stacktrace = std::basic_stacktrace<polymorphic_allocator<stacktrace_entry>>;

Expanded Motivation.

2 Before / After Table

char buffer[1024];

std::pmr::monotonic_buffer_resource pool{
    std::data(buffer), std::size(buffer)};

char buffer[1024];

std::pmr::monotonic_buffer_resource pool{
    std::data(buffer), std::size(buffer)};

std::pmr::stacktrace trace{&pool};

3 Motivation

The type std::basic_stacktrace is a class template with an allocator type parameter, the allocator is fixed to be std::allocator in the std::stacktrace alias, and it models AllocatorAwareContainer. All of the work to enable a pmr using type has been done, except for actually specifying the existence of the template alias std::pmr::stacktrace with a polymorphic_allocator.

In general a template should have an alias in the pmr namespace when the primary template supports the allocator model and the allocator template parameter is defaulted to be std::allocator. Providing the pmr alias is a convenience for changing the default.

Not having the pmr alias makes std::stacktrace inconsistent with the other types that support using a polymorphic_allocator. Programmers that wish to use types supporting polymorphic memory resources should expect to find those types having a pmr alias that changes the default. Although the burden is often low for writing such an alias, it is not zero.

In addition, writing into a fixed contiguous buffer can improve performance allowing this facilty to be used safely in more contexts.

4 Proposed Wording

Modify 20.21.2 [stacktrace.syn] as indicated

namespace std {
  // [stacktrace.entry], class stacktrace_­entry
  class stacktrace_entry;

  // [stacktrace.basic], class template basic_­stacktrace
  template<class Allocator>
    class basic_stacktrace;

  // basic_­stacktrace typedef names
  using stacktrace = basic_stacktrace<allocator<stacktrace_entry>>;

  // [stacktrace.basic.nonmem], non-member functions
  template<class Allocator>
    void swap(basic_stacktrace<Allocator>& a, basic_stacktrace<Allocator>& b)

  string to_string(const stacktrace_entry& f);

  template<class Allocator>
    string to_string(const basic_stacktrace<Allocator>& st);

  template<class charT, class traits>
    basic_ostream<charT, traits>&
      operator<<(basic_ostream<charT, traits>& os, const stacktrace_entry& f);

  template<class charT, class traits, class Allocator>
    basic_ostream<charT, traits>&
      operator<<(basic_ostream<charT, traits>& os, const basic_stacktrace<Allocator>& st);

  namespace pmr {
    using stacktrace = basic_stacktrace<polymorphic_allocator<stacktrace_entry>>;

  // [stacktrace.basic.hash], hash support
  template<class T> struct hash;
  template<> struct hash<stacktrace_entry>;
  template<class Allocator> struct hash<basic_stacktrace<Allocator>>;

Compiler Explorer Link