Question:
I want to storeboost::hana::map
inside a class so that I can write code like this inside the class:if constexpr (boost::hana::contains(map_, "name"_s)){
std::cout << map_["name"_s] << std::endl;
}
But I am getting an error:note: implicit use of ‘this’ pointer is only allowed within the evaluation of a call to a ‘constexpr’ member function
With
this->map_
:note: use of ‘this’ pointer is only allowed within the evaluation of a call to a ‘constexpr’ member function
The most interesting thing is if I write like this:
if (boost::hana::contains(map_, "name"_s)){
std::cout << map_["name"_s] << std::endl;
}
I get an error that there is no such field (it really may not exist, that’s why there was an if constexpr
).And yet, it works like this(WHY?!):
auto map = map_;
if constexpr (boost::hana::contains(map, "name"_s)){
std::cout << map["name"_s] << std::endl;
}
I absolutely don’t understand how it works. How is boost::hana::map
supposed to be used in this case?Full example
Stupid workaround
Answer:
Yes, usingthis
here appears to be right out.Since the information you are trying to access should be accessible at compile-time, one simple workaround would be to use the type of the entire expression.
void print_name()
{
if constexpr (decltype(boost::hana::contains(map, "name"_s)){})
{
Print(map["name"_s]);
}
}
Note that hana::contains
returns an object derived from std::integral_constant
so the contained value is static constexpr
, and it implicitly converts to bool
.If you have better answer, please add a comment about this, thank you!
Leave a Review