string_view
operator+
vs. StrCat()
absl::Status
std::bind
absl::optional
and std::unique_ptr
absl::StrFormat()
make_unique
and private
Constructors.bool
explicit
= delete
)switch
Statements Responsibly= delete
AbslHashValue
and Youcontains()
std::optional
parametersif
and switch
statements with initializersinline
Variablesstd::unique_ptr
Must Be MovedAbslStringify()
vector.at()
auto
for Variable DeclarationsDefine flags at global scope in a .cc
file. Declare them at most once in a
corresponding .h
file.
Using header files is a reflex for most of us, so we may have forgotten why they are used:
#include
elsewhere.
The entire program sees the same declaration..cc
that defines the same entity ensures the
definition matches declaration.You can do this incorrectly with no link-time error. First, place the following
in a .cc
file:
// Defining --my_flag in a .cc file.
ABSL_FLAG(std::string, my_flag, "", "My flag is a string.");
And the following, erroneous, declaration of the flag in a different .cc
file
(perhaps a test):
// Declared in error: type should be std::string.
extern absl::Flag<int64> FLAGS_my_flag;
The program is ill-formed, and whatever happens is the result of undefined behavior. In my test program, this code compiled, linked, and crashed when the flag was accessed.
Design with command line flags as you would global variables.
.h
file
corresponding to its definition.ABSL_FLAG(type, ...)
macro to define flags.Flags are global variables. Use them judiciously. Use and declare them as you would any other global variable.