Skip to content

STL: Consistently use empty braces to construct tags like _Meow{} #468

Description

In the STL, "tag dispatch" is one of our oldest and most common metaprogramming techniques. When we need to construct temporary tags, we had to say _Meow() in C++98/03. Now, we prefer to say _Meow{}, because this avoids looking like a function call. We're trying to follow that style in new code, but there's lots of existing code that should be updated.

For example, the _Zero_then_variadic_args_t and _One_then_variadic_args_t tag types:

STL/stl/inc/vector

Lines 444 to 450 in fd04f77

vector() noexcept(is_nothrow_default_constructible_v<_Alty>) : _Mypair(_Zero_then_variadic_args_t()) {
_Mypair._Myval2._Alloc_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Getal()));
}
explicit vector(const _Alloc& _Al) noexcept : _Mypair(_One_then_variadic_args_t(), _Al) {
_Mypair._Myval2._Alloc_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Getal()));
}

As I recall, the difference between T() and T{} can be detected by extremely unusual user code, so we shouldn't change how any user-defined types are constructed (e.g. comparison function objects). However, STL-internal tags can be freely changed.

Additionally, for STL-internal tag types, we should strongly consider giving them explicit default constructors, following modern practice in the Standard, which prevents unintentional misuse (e.g. passing {} where a tag is expected).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSomething can be improvedfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions