Boost.Hana  1.5.0
Your standard library for metaprogramming
boost::hana::literals Namespace Reference

Namespace containing C++14 user-defined literals provided by Hana. More...

Functions

template<char... c>
constexpr auto operator""_c ()
 Creates a hana::integral_constant from a literal. More...
 
template<typename CharT , CharT... s>
constexpr auto operator""_s ()
 Creates a compile-time string from a string literal. More...
 

Detailed Description

Namespace containing C++14 user-defined literals provided by Hana.

Function Documentation

template<char... c>
constexpr auto boost::hana::literals::operator""_c ( )

Creates a hana::integral_constant from a literal.

The literal is parsed at compile-time and the result is returned as a llong<...>.

Note
We use llong<...> instead of ullong<...> because using an unsigned type leads to unexpected behavior when doing stuff like -1_c. If we used an unsigned type, -1_c would be something like ullong<-1> which is actually ullong<something huge>.

Example

using namespace hana::literals; // contains the _c suffix
BOOST_HANA_CONSTANT_CHECK(1234_c == hana::llong_c<1234>);
BOOST_HANA_CONSTANT_CHECK(-1234_c == hana::llong_c<-1234>);
BOOST_HANA_CONSTANT_CHECK(1_c + (3_c * 4_c) == hana::llong_c<1 + (3 * 4)>);

References boost::hana::apply, and boost::hana::value.

template<typename CharT , CharT... s>
constexpr auto boost::hana::literals::operator""_s ( )

Creates a compile-time string from a string literal.

The string literal is parsed at compile-time and the result is returned as a hana::string. This feature is an extension that is disabled by default; see below for details.

Note
Only narrow string literals are supported right now; support for fancier types of string literals like wide or UTF-XX might be added in the future if there is a demand for it. See this issue if you need this.
Warning
This user-defined literal is an extension which requires a special string literal operator that is not part of the standard yet. That operator is supported by both Clang and GCC, and several proposals were made for it to enter C++17. However, since it is not standard, it is disabled by default and defining the BOOST_HANA_CONFIG_ENABLE_STRING_UDL config macro is required to get this operator. Hence, if you want to stay safe, just use the BOOST_HANA_STRING macro instead. If you want to be fast and furious (I do), define BOOST_HANA_CONFIG_ENABLE_STRING_UDL.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
using namespace hana::literals;
// By default, this is disabled
#ifdef BOOST_HANA_CONFIG_ENABLE_STRING_UDL
constexpr auto str = "Hello world!"_s;
BOOST_HANA_CONSTANT_CHECK(str == hana::string_c<'H', 'e', 'l', 'l', 'o', ' ',
'w', 'o', 'r', 'l', 'd', '!'>);
BOOST_HANA_CONSTANT_CHECK(hana::is_a<hana::string_tag>(str));
BOOST_HANA_CONSTANT_CHECK(hana::length(str) == hana::size_c<12>);
#endif
int main() { }

References boost::hana::apply, boost::hana::find, boost::hana::if_, boost::hana::lexicographical_compare, and boost::hana::value.