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

Created on 28 Jan 2020  路  6Comments  路  Source: microsoft/STL

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:
https://github.com/microsoft/STL/blob/fd04f77dd217a7502c77e0cbf45e24fcb84ed17c/stl/inc/vector#L444-L450

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).

enhancement fixed

All 6 comments

Can I work on this one @StephanTLavavej @CaseyCarter ?

Yep, go ahead! 馃樃

There is some files that I should focus on ?? or just update all files in stl/inc and stl/src ??

All files would be great; we have no particular focus here. stl/inc/cvt is lower priority, though.

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).

this means we should add explicit for default constructor, right ?
something like :
struct _Zero_then_variadic_args_t{ explicit _Zero_then_variadic_args_t() {}; }

Yes - your PR has the ideal form explicit _Not_fn_tag() = default;. (This tells the compiler to generate the definition, which may result in slightly better codegen.)

Was this page helpful?
0 / 5 - 0 ratings