Questions tagged [boost-hana]
Boost.Hana is a C++14-based metaprogramming library. It provides high level algorithms to manipulate heterogeneous sequences, allows writing type-level computations with a natural syntax, provides tools to introspect user-defined types and much more.
165
questions
2
votes
1answer
69 views
How to make std::optional a run-time monad with Hana?
The type hana::optional represents an
Optional value whose optional-ness is known at compile-time
and it models, among others, the concept of Monad. Since it is a compile-time optional, the question ...
2
votes
2answers
52 views
Combining predicates in a functional way and allowing short-circuiting to work
Foreword
I asked a similar question: say I have a predicate auto p1 = [](int x){ return x > 2; } and a predicate auto p2 = [](int x){ return x < 6; }, how do I combine p1 and p2 to obtain p1and2 ...
1
vote
1answer
54 views
Why does Boost.Hana use macros for opening/closing namespaces?
One of the many examples of two macros being used to open and close a namespace respectively is in /usr/include/boost/hana/transform.hpp:
BOOST_HANA_NAMESPACE_BEGIN
// stuff
BOOST_HANA_NAMESPACE_END
...
2
votes
2answers
60 views
Clang compiler error when using boost::hana Y-combinator
I have the following implementation of an AST, built using C++17's std::variant type, on which I would like to apply a visitor recursively. I have done this with the help of some utilities from Boost'...
0
votes
1answer
36 views
Perfect Forwarding for Lambda Chaining
I want to chain/compose multiple lambdas to a new lambda. When calling the new lambda with parameters it should call the original lambdas with these parameters. This lambda chaining does work well ...
1
vote
1answer
68 views
Compile time efficient remove duplicates from a boost::hana tuple
I use the boost::hana to_map function to remove duplicates from boost::hana tuple of types. See it at the compiler explorer. The code works very well but compiles very long (~10s). I wonder if there ...
1
vote
2answers
43 views
decltype on BOOST_HANA_STRING
My objective is to have a something like the following while writing the string "hello" as a regular string.
struct hello: element<hana::string<'h', 'e', 'l', 'l', 'o'>, std::...
2
votes
1answer
82 views
Function ignoring its argument(s)
This is mostly a curiosity question.
I saw code like this (I'm not interested in what it does)
constexpr auto xxx = boost::hana::overload(
[](SomeType& x){ /* x is used and something is ...
1
vote
1answer
57 views
Combining predicates in a functional way
Does Boost Hana provide a way to combine predicates with logical operators?
I'm referring to something roughly like this
constexpr auto both = [](auto&& f, auto&& g){
return [&...
0
votes
0answers
38 views
BOOST::HANA get all member names and iterate
Coming from a Java/C# background and as a BOOST newbie, I have difficulty in getting a list of the class members.
I have the following struct/class.
class Configurations
{
public:
...
1
vote
3answers
105 views
Base case specialization for recursive variadic template
My target is to define a Recursive class, templated on an int N and one or more types T, ...Ts, which should behave like a std::pair with
a std::array of N items of type T as the first,
and, as ...
0
votes
1answer
53 views
boost::hana::is_valid fails to compile with gcc8 (and more) and --std=c++14
I use this code with std=c++14 and gcc7.3:
#include <iostream>
#include <string>
#include <type_traits>
#include <boost/hana/assert.hpp>
#include <boost/hana/equal.hpp>
#...
1
vote
2answers
74 views
Compile size of a struct minus padding recusively
I have a similar Problem as in this question.
I want to get the size of struct at compile time, including all the substructs without the compiler specific padding added.
struct Bar {
...
0
votes
0answers
43 views
boost::hana::length fails to give a constexpr result when using references
In the following function:
namespace hana = boost::hana;
template<typename T>
constexpr auto f(const T& obj){
constexpr int size = hana::length(obj);
}
When called e.g. with f(hana::...
1
vote
1answer
81 views
How can I make the std::vector class a Sequence so that it can be passed to boost::hana::group?
I want the following code to compile and work:
#include <boost/hana/group.hpp>
#include <functional>
#include <vector>
int main() {
std::vector<int> x = {1,1,3,4};
auto ...
0
votes
1answer
93 views
Understanding Boost.Hana Quick start
I'm going through Boost.Hana's User Manual to learn more about template meta programming and functional programming in C++.
As regards the Real world example, I still miss a few bits, all concentrated ...
3
votes
2answers
77 views
Taking n lines from a file which contains m lines, repeating the file (lazily) if necessary
I'm looking for a way to use a text file to generate as much text as I need by simply repeating the as few times as needed.
In a purely functional language, like Haskell, the solution seems trivial: ...
2
votes
1answer
72 views
How to wrap (compose) a boost hana map and access the bracket operator (operator[])?
I'm using a hana map (created using hana::make_map). I have a very simple class that would inherit from the hana map, and expose a second map as such
auto values = hana::make_map( ... );
auto ...
1
vote
1answer
290 views
boost::hana how to check if a type is std::optional?
I searched the boost::hana::traits I cannot find anything related to checking concept of types. For example is_vector, is_optional. Is there any tools in hana can simplify this? Moreover, I would like ...
1
vote
2answers
34 views
is there any way to index the hana adapted struct by its member name?
I have a hana defined struct, I knew I can iterate through it, but I wonder how can I index it by member name?
#include <string>
#include <boost/hana.hpp>
int main()
{
struct Person {
...
1
vote
1answer
106 views
Not a constant expression in lambda function
I have the following code:
#include <boost/hana.hpp>
#include <array>
#include <iostream>
#include <utility>
namespace hana = boost::hana;
#define HEADER_CONNECT 0b00010000
#...
0
votes
2answers
81 views
Why do I get unexpected index when looping a boost::hana::tuple
Here is my code
using my_variant = std::variant<std::uint32_t, std::int32_t>;
auto my_types_map = hana::make_tuple(
hana::make_pair(hana::type_c<std::uint32_t>, hana::integral_c<std:...
0
votes
1answer
66 views
How to transform a boost::hana::map into lambdas
I have the following code
template <typename T>
void my_func(T& /*var*/)
{
};
auto my_types = hana::make_map(
hana::make_pair(hana::type_c<std::uint32_t>, hana::integral_c<std::...
0
votes
3answers
98 views
How to convert a boost::hana::tuple into a std::variant
I have the following code where I want to convert a boost::hana::tuple into a std::variant
namespace hana = boost::hana;
template <typename Tuple>
struct to_variant;
template <typename... ...
3
votes
1answer
117 views
Boost Hana Implement a custom Sequence
I'm facing a situation where I usually would create a custom class by inheriting from boost::hana::tuple. For example, by using the following code,
template<typename ... args>
struct ...
0
votes
1answer
39 views
Boost.Hana: Transform tuple of values to tuple of corresponding types in constexpr context
Recently I had to write a transformation from a tuple of values to the (constexpr) tuple of the types of the values.
namespace hana = boost::hana;
// we have this (not neccessarily constexpr)
auto ...
2
votes
1answer
88 views
Boost::Hana tuple, best way to modify a value
I'm pretty new to this powerful library. I was surprised I couldn't find an easy way to, given a tuple, return another tuple that just modifies a single element, by index.
Moreover, I want this ...
9
votes
3answers
478 views
How to solve the issue of “read of non-constexpr variable 'a' is not allowed in a constant expression” with boost.hana
I'm using c++17 with Boost.hana to write some meta-programming programs. One issue stuck me is what kind of expression can be used in a constexpr context like static_assert. Here is an example:
#...
0
votes
1answer
50 views
Apply a runtime function only when the runtime input equals a named member in a set of types
I have a hana tuple of types
auto mytypes =\
bh::make_tuple(bh::type_c<T1>,
bh::type_c<T2>,
bh::type_c<T3>,
bh::type_c<T4>);
Each of the T are ...
2
votes
1answer
187 views
Define struct having more than 40 fields using BOOST_HANA_DEFINE_STRUCT in Boost::Hana
Defining a struct that has more than 40 fields using macro BOOST_HANA_DEFINE_STRUCT in library Boost::hana, for example,
#include <boost/hana/define_struct.hpp>
namespace hana = boost::hana;
//...
2
votes
3answers
157 views
How do I implement equality compare operators for Boost.Hana Structs?
Let's say we have Boost.Hana Struct:
struct SomeStruct {
BOOST_HANA_DEFINE_STRUCT(SomeStruct, (int, x), (double, y), (char, c));
};
I would like to compare SomeStruct as s1 == s2 and s1 != s2.
If ...
0
votes
1answer
49 views
Passing templated constexpr to function inferring type of auto object
I'm making an entity-component system library utilizing template metaprogramming to evaluate signature bitset data at compile-time and allow for precise bitset size without using #define ...
3
votes
1answer
158 views
Boost.Hana JSON example: Difference between string and decltype(std::to_string(…))
I just read the Boost.Hana tutorial but unfortunately got stuck very early. Could anybody explain to me why to_json for integers is implemented the way it is:
template <typename T>
auto to_json(...
2
votes
1answer
74 views
How to check if method exists when arguments are given by the tuple?
I would like to check whether a function with the given signature exists. The twist is that the signature is given by the tuple:
#include <boost/hana/type.hpp>
#include <boost/hana/tuple.hpp&...
4
votes
0answers
104 views
boost::hana::to_map combined with boost::hana::transform segfaults because of 'global-buffer-overflow'
Having a simple program with the following source code,
f1.cpp:
#include "to_map.hpp"
using namespace boost::hana::literals;
void f1() {
auto tmp = boost::hana::make_tuple(
boost::hana:...
2
votes
1answer
117 views
Can a std::array be unpacked into a non-type template parameter pack with Boost.Hana?
template<int...>
struct S {};
constexpr auto a = std::array{0, 1, 2};
I would like to unpack the elements of a as template arguments to S. That is; S<0, 1, 2>.
A possible C++2a ...
1
vote
1answer
175 views
How to introspect into boost::hana::map<…> when doing SFINAE, without having a (constexpr) instance of it?
Is it possible to check for key existence or even value type, without accessing a boost::hana::map<...> object, but instead having only its type? (To perform SFINAE.)
Here's a code, that ...
2
votes
1answer
112 views
Boost.Hana test if lambda is callable with particular argument
I want to test if a callable lambda can be called with particular arguments by using Boost.Hana. The compiler complains that callable expects 0 arguments but 1 were provided. Isn't the if_ a compile ...
1
vote
0answers
112 views
When is a char array a constant expression?
I've been using Boost.Hana to generate compile-time strings for use as template parameters with gcc:
using namespace boost::hana::literals;
#define GQL_STR(tok) decltype(#tok ## _s)
It relies on ...
1
vote
1answer
259 views
How to properly check for member at compile time with boost::hana?
I am writing a physics simulation program and I want to do the following:
I have a hana adapted struct and I want to check if this struct has member called "absorbedEnergy" at compile time using:
if ...
2
votes
3answers
134 views
Iterate over template int
I have a function:
template<class Real, int N>
constexpr std::array<Real, N> get_array();
and I would like to test it over many types and many integers. In pseudocode:
auto types = {...
1
vote
1answer
100 views
How to join two Boost Hana maps?
I have two boost::hana::maps to join together.
constexpr auto m1 = hana::make_map(
hana::make_pair("key1"_s, hana::type_c<std::string>),
hana::make_pair("key2"_s, hana::type_c<std::...
4
votes
2answers
205 views
How to assign to members of a struct object?
I'm taking my first steps with Boost.Hana, so please bear with me. I have
#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace hana::literals;
#include <string>
...
1
vote
2answers
178 views
How to reverse a hana::string
Does anyone know how to reverse a boost::hana::string?
The following does not work:
#include <boost/hana.hpp>
#include <boost/hana/reverse.hpp>
auto s = BOOST_HANA_STRING("abc");
auto s2 ...
1
vote
1answer
88 views
How to retrieve types from boost::hana::tuple_t?
I have using hana:tuple_t to store and retrieve a typelist:
auto Types = hana::tuple_t<int, double>;
auto first_t = Types[hana::int_c<0>];
static_assert(std::is_same_v<decltype(first_t)...
0
votes
1answer
38 views
How to tranform a hana::tuple_t with an arbitrary type meta-function?
Lets say I have two types each having an internal type:
struct A1{
using type = int;
};
struct B1{
using type = double;
};
I created a hana::tuple_t from my classes (e.g. auto Types = hana::...
1
vote
1answer
118 views
Is it possible to convert integral_constant to hana::string
I would like to be able to generate a compile-time string that contains a compile-time integer, like this. Is it possible?
I know I can insert a single elements to the boost::string using fold_left, ...
0
votes
1answer
131 views
boost::hana: Why can't I filter a set?
I'm using boost::hana, and I'd like to filter a boost::hana::set.
#include <boost/hana.hpp>
#include <type_traits>
int main(){
using namespace boost::hana;
auto a = make_set(1,'a'...
2
votes
1answer
112 views
boost hana index_if and type
I wonder why, in this code, the type of i is an empty optional.
auto t = boost::hana::make_tuple(boost::hana::type_c<int>, boost::hana::type_c<double>);
auto i = boost::hana::index_if(t, ...
1
vote
1answer
187 views
How can one modify a member of a boost hana struct
I can see how one can easily read from an introspective boost hana struct generically by field/value, but I cant find any way to write into a struct generically.
Basically I would want to be able to ...