4.6.0
Freundlich's C++ toolkit
|
Literals or polymorphic integer constants.
In C++, most literals are polymorphic in a sense that they can be implicitly converted to different types. For example, 0
can be used in places where nullptr
can or to initialize a float to 0.f
. Using literals like this, however, doesn't work to initialize integer-like types with explicit constructors or types that require special factory functions. This can quickly become a problem in generic code. Consider a generic function that tries to divide its argument by two.
A lot of types provide an operator/
but they can't be implicitly converted from literals, for example strong typedefs.
The code above obviously doesn't work, so you might conclude from this example that we should change the function half to say T{2}
instead of 2
. However, other types like units without quantities in Boost.Units can only be initialized by a special factory function. Therefore, fcppt provides a customization point which is used to create objects from literals. For example, we could have found the following type inside a library:
To make fcppt::literal work with this type, we need to provide a specialization for fcppt::make_literal.
In order to make the half
function work, we call fcppt::literal.
We can then call our half_2
function with custom_type
int
with fcppt::literal but this isn't enforced by the type system because it might be too restrictive. Classes | |
struct | fcppt::make_literal< Type, Enable > |
struct | fcppt::make_literal< fcppt::strong_typedef< Type, Tag > > |
Functions | |
template<typename Type , typename Arg > | |
constexpr fcppt::make_literal< Type >::decorated_type | fcppt::literal (Arg const &&_integral) noexcept |
Creates a literal of a type. | |
|
constexprnoexcept |
Creates a literal of a type.
Creates a literal of type Type from the value _integral, using fcppt::make_literal to do any conversions if necessary.
Type | The literal type to create. Must be a value type. See fcppt::type_traits::is_value. |
Arg | An arithmetic type. |