Git Product home page Git Product logo

Comments (2)

pfultz2 avatar pfultz2 commented on May 9, 2024

is_true and is_false work nice, but I find myself doing n-ary std::integral_constant<bool, A::value || B::value || ...>

There is is_true_c and is_false_c that take a boolean directly, so you don't have to wrap it in an integral constant. So you should just be able to write is_true_c<(A() || B() || ...)> directly.

The usual n-ary or_ metafunction should do the trick.

I would prefer not to add that to this library, since this is a traits library and not a metaprogramming library. I believe Eric Niebler's meta library has an or_ metafunction.

In many cases, a concept requirement is not a set of mandatory properties in a way that all of them should be accomplished by the type, but a set of possible properties.

Usually when you need an or of the type requirements is when overloading is involved. However, you can just check if the expression is valid instead. For example, a generic print that works for any streamable type as well as for tuples and ranges could be written like this using the Fit library:

FIT_STATIC_FUNCTION(print) = fit::conditional(
    [](const auto& x) -> decltype(std::cout << x, void())
    {
        std::cout << x << std::endl;
    },
    [](const auto& range) -> decltype(std::cout << *adl::adl_begin(range), void())
    {
        for(const auto& x:range) std::cout << x << std::endl;
    },
    [](const auto& tuple) -> decltype(for_each_tuple(tuple, fit::identity), void())
    {
        return for_each_tuple(tuple, [](const auto& x)
        {
            std::cout << x << std::endl;
        });
    }
);

Rather than check with or that it meets the requirements of each overload, I can instead just check if print is a valid expression:

TICK_TRAIT(is_printable)
{
    template<class T>
    auto require(const T& x) -> decltype(print(x));
};

from tick.

sbergen avatar sbergen commented on May 9, 2024

Just as a note for anyone else browsing this: C++17 has std::disjunction for logical or on type traits. You can easily replicate it if your compiler doesn't have C++17 support, see: http://en.cppreference.com/w/cpp/types/disjunction

Also, would be nice if is_true_c and is_false_c would be mentioned in the documentation: I had a really hard time finding them...

from tick.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.