Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using boost::hana for introspection

I'm walking through the examples of the help page of the awesome boost::hana library and am not able to get the introspection example working correctly.

This code is intended to check at compile time whether an object has a specific member function or not and then uses this member function or does something default.

So I declared these two types:

struct WithoutToString
{ };

struct WithToString
{
    std::string toString()
    {
        return "implements toString()";
    }
};

This is the 1st version of the check using hana::is_valid:

auto has_toString = hana::is_valid([] (auto&& obj) -> decltype(obj.toString()) { });

template <typename T>
std::string optionalToString1(T const& obj)
{
    return hana::if_(has_toString(obj),
        [] (auto& x) { return x.toString(); },
        [] (auto& x) { return "toString not defined"; }
    )(obj);
}

This is the 2nd version of the check using hana::sfinae:

template <typename T>
std::string optionalToString2(T const& obj)
{
    auto maybeToString = hana::sfinae([](auto&& x) -> decltype(x.toString())
    {
        return x.toString();
    });

    return maybeToString(obj).value_or("toString not defined");
}

Using both versions like this...

int main()
{
    WithToString obj;

    std::cout << optionalToString1(obj);
    std::cout << optionalToString2(obj);

    return 0;
}

...always shows "toString not defined" instead of "implements toString()".

Note: checking obj with static_assert(has_toString(obj), "Does not implement toString()."); shows the right behaviour.

Am I missing something? Or is it a compiler (clang 5.0.1) or library (boost 1.66) issue?

Thank you.

like image 792
Wum Avatar asked Nov 24 '25 19:11

Wum


1 Answers

Your optionalToStringX functions take T const&. The toString isn't a const-qualified member function, so it isn't applicable.

Live On Coliru

#include <boost/hana.hpp>
#include <string>

namespace hana = boost::hana;

struct WithoutToString { }; 
struct WithToString { std::string toString() const { return "implements toString()"; } };

namespace v1 {
    //This is the 1st version of the check using hana::is_valid:
    auto has_toString = hana::is_valid([] (auto&& obj) -> decltype(obj.toString()) { });

    template <typename T>
        std::string optionalToString1(T const& obj)
        {
            return hana::if_(has_toString(obj),
                    [] (auto&& x) { return std::forward<decltype(x)>(x).toString(); },
                    [] (auto&&) { return "toString not defined"; }
                    )(obj);
        }
}

namespace v2 {
    //This is the 2nd version of the check using hana::sfinae:
    template <typename T>
        std::string optionalToString2(T const& obj)
        {
            auto maybeToString = hana::sfinae([](auto&& x) -> decltype(x.toString())
                    {
                    return x.toString();
                    });

            return maybeToString(obj).value_or("toString not defined");
        }
}

#include <iostream>
int main()
{
    WithToString with;
    WithoutToString without;

    std::cout << std::boolalpha << v1::has_toString(without) << std::endl;
    std::cout << std::boolalpha << v1::has_toString(with)    << std::endl;

    std::cout << v1::optionalToString1(without) << std::endl;
    std::cout << v1::optionalToString1(with)    << std::endl;
    std::cout << v2::optionalToString2(without) << std::endl;
    std::cout << v2::optionalToString2(with)    << std::endl;
}

Prints

true
false
implements toString()
toString not defined
implements toString()
toString not defined
like image 90
sehe Avatar answered Nov 26 '25 11:11

sehe