template <typename T, typename Tag>
class NamedType
Defined at line 43 of file ../../third_party/cobalt/src/lib/util/named_type.h
NamedType is used to wrap types (such as uint32_t) to allow them to be
disambiguated from other instances of the same type.
Note: this class is based on the idea discussed in
https://www.fluentcpp.com/2017/05/05/news-strong-types-are-free
Example:
void do_something(int height, int width);
int height = 10;
int width = 10;
do_something(width, height); // Wrong order, but it still compiles!
using Height = NamedType
<int
, struct HeightTag>;
using Width = NamedType
<int
, struct WidthTag>;
void do_something(Height height, Width width);
Height height(10);
Width width(10);
do_something(width, height); // Wrong order, but compile error!
Note: The 'Tag' is a uique empty struct. If two instances of
NamedType in the same namespace use the same struct name, they will
be considered the same type. This is likely undesirable, so the Tag
struct should be unique.
Public Methods
void NamedType<T, Tag> (T value)
Explicit constructor from base type. (e.g. Height(10))
Defined at line 46 of file ../../third_party/cobalt/src/lib/util/named_type.h
template <typename T_ = T, typename = IsNotReference<T_>>
void NamedType<T, Tag> (T && value)
Move constructor. This is only available for instances of NamedType
where the base type T is not a reference.
Defined at line 51 of file ../../third_party/cobalt/src/lib/util/named_type.h
T & get ()
Access the inner value of NamedType mutably.
Defined at line 54 of file ../../third_party/cobalt/src/lib/util/named_type.h
const std::remove_reference_t<T> & get ()
Access the inner value of NamedType immutably. (remove_reference_t
in the case where the contained type is a reference, this will
return a copy of the contained value).
Defined at line 59 of file ../../third_party/cobalt/src/lib/util/named_type.h
NamedType<T, Tag> operator+ (const NamedType<T, Tag> & other)
Addition
Defined at line 62 of file ../../third_party/cobalt/src/lib/util/named_type.h
NamedType<T, Tag> & operator+= (const NamedType<T, Tag> & other)
Defined at line 65 of file ../../third_party/cobalt/src/lib/util/named_type.h
NamedType<T, Tag> operator+ ()
Defined at line 69 of file ../../third_party/cobalt/src/lib/util/named_type.h
NamedType<T, Tag> operator- (const NamedType<T, Tag> & other)
Subtraction
Defined at line 72 of file ../../third_party/cobalt/src/lib/util/named_type.h
NamedType<T, Tag> & operator-= (const NamedType<T, Tag> & other)
Defined at line 75 of file ../../third_party/cobalt/src/lib/util/named_type.h
NamedType<T, Tag> operator- ()
Defined at line 79 of file ../../third_party/cobalt/src/lib/util/named_type.h
void print (std::ostream & os)
Printing
Defined at line 82 of file ../../third_party/cobalt/src/lib/util/named_type.h
Friends
template <typename Ttypename Tag>
std::ostream & NamedType (std::ostream & osconst NamedType<T, Tag> & other)