Hashable
hana::hash(hana::type_c<int>),
hana::type_c<int>
hana::hash(hana::type_c<void>),
hana::type_c<void>
int
main() { }
constexpr auto equal
Returns a Logical representing whether x is equal to y.
Definition:
equal.hpp:64
Defines boost::hana::hash.
template<typename T >
constexpr
type
< T >
type_c
{}
Creates an object representing the C++ type
T
.
constexpr auto
decltype_
= see documentation
decltype
keyword, lifted to Hana.
More...
constexpr auto
typeid_
= see documentation
Returns a
hana::type
representing the type of a given object.
More...
template<>
constexpr auto
make< type_tag >
= hana::decltype_
Equivalent to
decltype_
, provided for convenience.
More...
constexpr auto
make_type
=
hana::make
<
type_tag
>
Equivalent to
make<
type_tag
>
, provided for convenience.
More...
constexpr auto
sizeof_
sizeof
keyword, lifted to Hana.
More...
constexpr auto
alignof_
alignof
keyword, lifted to Hana.
More...
constexpr auto
is_valid
Checks whether a SFINAE-friendly expression is valid.
More...
Friends
template<typename X , typename Y >
constexpr friend auto
operator==
(X &&x, Y &&y)
Equivalent to
hana::equal
template<typename X , typename Y >
constexpr friend auto
operator!=
(X &&x, Y &&y)
Equivalent to
hana::not_equal
Public Member Functions
constexpr auto
operator+
() const
Returns rvalue of self. See
description
.
◆
decltype_
decltype
keyword, lifted to Hana.
-
Deprecated:
-
The semantics of
decltype_
can be confusing, and
hana::typeid_
should be preferred instead.
decltype_
may be removed in the next major version of the library.
decltype_
is somewhat equivalent to
decltype
in that it returns the type of an object, except it returns it as a
hana::type
which is a first-class citizen of Hana instead of a raw C++ type. Specifically, given an object
x
,
decltype_
satisfies
constexpr auto decltype_
decltype keyword, lifted to Hana.
Definition:
type.hpp:190
constexpr type< T > type_c
Creates an object representing the C++ type T.
Definition:
type.hpp:128
As you can see,
decltype_
will strip any reference from the object's actual type. The reason for doing so is explained below. However, any
cv
-qualifiers will be retained. Also, when given a
hana::type
,
decltype_
is just the identity function. Hence, for any C++ type
T
,
In conjunction with the way
metafunction
& al. are specified, this behavior makes it easier to interact with both types and values at the same time. However, it does make it impossible to create a
type
containing another
type
with
decltype_
. In other words, it is not possible to create a
type_c<decltype(type_c<T>)>
with this utility, because
decltype_(type_c<T>)
would be just
type_c<T>
instead of
type_c<decltype(type_c<T>)>
. This use case is assumed to be rare and a hand-coded function can be used if this is needed.
Rationale for stripping the references
The rules for template argument deduction are such that a perfect solution that always matches
decltype
is impossible. Hence, we have to settle on a solution that's good and and consistent enough for our needs. One case where matching
decltype
's behavior is impossible is when the argument is a plain, unparenthesized variable or function parameter. In that case,
decltype_
's argument will be deduced as a reference to that variable, but
decltype
would have given us the actual type of that variable, without references. Also, given the current definition of
metafunction
& al., it would be mostly useless if
decltype_
could return a reference, because it is unlikely that
F
expects a reference in its simplest use case:
int
i = 0;
auto
result = metafunction<F>(i);
Hence, always discarding references seems to be the least painful solution.
Example
struct
X { };
static
int
const
& i = 1;
int
main() { }
Returns a
hana::type
representing the type of a given object.
hana::typeid_
is somewhat similar to
typeid
in that it returns something that represents the type of an object. However, what
typeid
returns represent the
runtime
type of the object, while
hana::typeid_
returns the
static
type of the object. Specifically, given an object
x
,
typeid_
satisfies
typeid_
(x) ==
type_c
<decltype(x) with ref and cv-qualifiers stripped>
constexpr auto typeid_
Returns a hana::type representing the type of a given object.
Definition:
type.hpp:233
As you can see,
typeid_
strips any reference and cv-qualifier from the object's actual type. The reason for doing so is that it faithfully models how the language's
typeid
behaves with respect to reference and cv-qualifiers, and it also turns out to be the desirable behavior most of the time. Also, when given a
hana::type
,
typeid_
is just the identity function. Hence, for any C++ type
T
,
In conjunction with the way
metafunction
& al. are specified, this behavior makes it easier to interact with both types and values at the same time. However, it does make it impossible to create a
type
containing another
type
using
typeid_
. This use case is assumed to be rare and a hand-coded function can be used if this is needed.
Example
#include <string>
struct
Cat { std::string name; };
struct
Dog { std::string name; };
struct
Fish { std::string name; };
bool
operator==
(Cat
const
& a, Cat
const
& b) {
return
a.name == b.name; }
bool
operator!=
(Cat
const
& a, Cat
const
& b) {
return
a.name != b.name; }
bool
operator==
(Dog
const
& a, Dog
const
& b) {
return
a.name == b.name; }
bool
operator!=
(Dog
const
& a, Dog
const
& b) {
return
a.name != b.name; }
bool
operator==
(Fish
const
& a, Fish
const
& b) {
return
a.name == b.name; }
bool
operator!=
(Fish
const
& a, Fish
const
& b) {
return
a.name != b.name; }
int
main() {
hana::tuple<Cat, Fish, Dog, Fish> animals{
Cat{
"Garfield"
}, Fish{
"Jaws"
}, Dog{
"Beethoven"
}, Fish{
"Nemo"
}
auto
mammals = hana::remove_if(animals, [](
auto
const
& a) {
return
hana::typeid_(a) == hana::type<Fish>{};
#define BOOST_HANA_RUNTIME_CHECK(...)
Equivalent to BOOST_HANA_RUNTIME_ASSERT, but not influenced by the BOOST_HANA_CONFIG_DISABLE_ASSERTIO...
Definition:
assert.hpp:209
Defines boost::hana::remove_if.
constexpr friend auto operator==(X &&x, Y &&y)
Equivalent to hana::equal
Definition:
comparable.hpp:36
constexpr friend auto operator!=(X &&x, Y &&y)
Equivalent to hana::not_equal
Definition:
comparable.hpp:47
Defines boost::hana::tuple.
Example
struct
X { };
int
main() { }
Defines boost::hana::make.
Example
struct
X { };
int
main() { }
Initial value:
= [](
auto
&& x) {
using
T =
typename
decltype(hana::decltype_(x))::type;
return
hana::size_c<
sizeof
(T)>;
sizeof
keyword, lifted to Hana.
sizeof_
is somewhat equivalent to
sizeof
in that it returns the size of an expression or type, but it takes an arbitrary expression or a
hana::type
and returns its size as an
integral_constant
. Specifically, given an expression
expr
,
sizeof_
satisfies
sizeof_
(expr) ==
size_t
<
sizeof
(decltype(expr) with references stripped)>
constexpr auto sizeof_
sizeof keyword, lifted to Hana.
Definition:
type.hpp:290
However, given a
type
,
sizeof_
will simply fetch the size of the C++ type represented by that object. In other words,
sizeof_
(type_c<T>) ==
size_t
<
sizeof
(T)>
The behavior of
sizeof_
is consistent with that of
decltype_
. In particular, see
decltype_
's documentation to understand why references are always stripped by
sizeof_
.
Example
struct
X { };
static_assert(hana::sizeof_(hana::type_c<X>) ==
sizeof
(X),
""
);
static_assert(hana::sizeof_(1) ==
sizeof
(1),
""
);
static_assert(hana::sizeof_(hana::type_c<int>) ==
sizeof
(
int
),
""
);
int
main() {}
Initial value:
= [](
auto
&& x) {
using
T =
typename
decltype(hana::decltype_(x))::type;
return
hana::size_c<
alignof
(T)>;
alignof
keyword, lifted to Hana.
alignof_
is somewhat equivalent to
alignof
in that it returns the alignment required by any instance of a type, but it takes a
type
and returns its alignment as an
integral_constant
. Like
sizeof
which works for expressions and type-ids,
alignof_
can also be called on an arbitrary expression. Specifically, given an expression
expr
and a C++ type
T
,
alignof_
satisfies
alignof_
(expr) ==
size_t
<
alignof
(decltype(expr) with references stripped)>
alignof_
(type_c<T>) ==
size_t
<
alignof
(T)>
constexpr auto alignof_
alignof keyword, lifted to Hana.
Definition:
type.hpp:326
The behavior of
alignof_
is consistent with that of
decltype_
. In particular, see
decltype_
's documentation to understand why references are always stripped by
alignof_
.
Example
struct
X { };
static_assert(hana::alignof_(hana::type_c<X>) ==
alignof
(X),
""
);
static_assert(hana::alignof_(1) ==
alignof
(decltype(1)),
""
);
static_assert(hana::alignof_(hana::type_c<int>) ==
alignof
(
int
),
""
);
int
main() { }
Initial value:
= [](
auto
&& f) {
return
[](
auto
&& ...args) {
return
whether f(args...) is a valid expression;
Checks whether a SFINAE-friendly expression is valid.
Given a SFINAE-friendly function,
is_valid
returns whether the function call is valid with the given arguments. Specifically, given a function
f
and arguments
args...
,
is_valid
(f, args...) == whether f(args...) is valid
constexpr auto is_valid
Checks whether a SFINAE-friendly expression is valid.
Definition:
type.hpp:369
The result is returned as a compile-time
Logical
. Furthermore,
is_valid
can be used in curried form as follows:
This syntax makes it easy to create functions that check the validity of a generic expression on any given argument(s).
-
Warning
-
To check whether calling a nullary function
f
is valid, one should use the
is_valid(f)()
syntax. Indeed,
is_valid(f /* no args */)
will be interpreted as the currying of
is_valid
to
f
rather than the application of
is_valid
to
f
and no arguments.
Example
#include <string>
#include <vector>
int
main() {
struct
Person { std::string name; };
auto
has_name = hana::is_valid([](
auto
&& p) -> decltype((
void
)p.name) { });
Person joe{
"Joe"
};
static_assert(has_name(joe),
""
);
static_assert(!has_name(1),
""
);
auto
has_value_type = hana::is_valid([](
auto
t) -> hana::type<
typename
decltype(t)::type::value_type
> { });
static_assert(has_value_type(hana::type_c<std::vector<int>>),
""
);
static_assert(!has_value_type(hana::type_c<Person>),
""
);
Defines boost::hana::not_.