boostorg / hana

Your standard library for metaprogramming
http://boostorg.github.io/hana
Boost Software License 1.0
1.69k stars 215 forks source link

How to use hana containers as class data members? #499

Closed impugachev closed 1 year ago

impugachev commented 2 years ago

For example, I want to write something like this in a class method.

if constexpr (boost::hana::contains(this->map_, "name"_s)){
    std::cout << this->map_["name"_s] << std::endl;
}

Unfortunately, c++ prohibits the use of this in if constexpr. How am I supposed to use hana here? Sorry if this has already been asked, I couldn't find anything similar.

Full example Stupid workaround

ldionne commented 1 year ago

Here's another way to do it: https://godbolt.org/z/W41K57cb6

The fundamental limitation here is that the language sees you are reading this->... inside a constant evaluated context and it bails out.