简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM