Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:17:17+00:00 2026-05-30T05:17:17+00:00

Consider the following attempt at a Boost.MPL style metaprogramming version of std::any_of #include <iostream>

  • 0

Consider the following attempt at a Boost.MPL style metaprogramming version of std::any_of

    #include <iostream>                     // cout
    #include <type_traits>                  // is_base_of, is_pod
    #include <boost/mpl/apply.hpp>          // apply
    #include <boost/mpl/fold.hpp>           // fold
    #include <boost/mpl/lambda.hpp>         // lambda, _1, _2
    #include <boost/mpl/logical.hpp>        // and_, true_   
    #include <boost/mpl/vector.hpp>         // vector

    template
    <
            typename Sequence,
            typename Pred
    >
    struct all_of
    :
            boost::mpl::fold<
                    Sequence,
                    boost::mpl::true_,
                    boost::mpl::lambda<
                            boost::mpl::and_<
                                    boost::mpl::_1,
                                    boost::mpl::apply< Pred, boost::mpl::_2 >
                            >
                    >
            >
    {};

    typedef int P1; typedef char P2; typedef float P3;

    typedef boost::mpl::vector<
            P1, P2, P3
    > pod_types;

    struct B {}; struct D1: B {}; struct D2: B {}; struct D3: B {};

    typedef boost::mpl::vector<
            D1, D2, D3
    > derived_types;

    int main() 
    {
            std::cout << (std::is_pod<P1>::value) << '\n';  // true
            std::cout << (std::is_pod<P2>::value) << '\n';  // true
            std::cout << (std::is_pod<P3>::value) << '\n';  // true       

            std::cout << (
                    all_of<
                            pod_types, 
                            std::is_pod< boost::mpl::_1 >                        
                    >::type::value  // true
            ) << '\n';

            std::cout << (std::is_base_of<B, D1>::value) << '\n';   // true
            std::cout << (std::is_base_of<B, D2>::value) << '\n';   // true
            std::cout << (std::is_base_of<B, D3>::value) << '\n';   // true

            std::cout << (
                    all_of<
                            derived_types, 
                            std::is_base_of< B, boost::mpl::_1 >    
                    >::type::value  // false (but should be true)
            ) << '\n';

            return 0;
    }

This prints out: 1 1 1 1 1 1 1 0. I.e., the final call to all_of with std::is_base_of passed as a predicate generates false. Why does this not work? Apperently, the base class B does not get properly bound to the predicate. How should I pass a binary predicate? Some combination of mpl::lambda or mpl::bind?

UPDATE

Based on Luc Touraille’s excellent answer, here is the lambda-free solution to my question, with as an added bonus the compile-time versions of none_of and any_of

    template<typename Sequence, typename Pred>
    struct all_of
    :
            std::is_same< typename 
                    boost::mpl::find_if<
                            Sequence,
                            boost::mpl::not_<Pred>
                    >::type, typename 
                    boost::mpl::end<Sequence>::type
            >
    {};

    template<typename Sequence, typename Pred>
    struct none_of
    :
            all_of< Sequence, boost::mpl::not_< Pred > >
    {};

    template<typename Sequence, typename Pred>
    struct any_of
    :
            boost::mpl::not_< none_of< Sequence, Pred > >
    {};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:17:19+00:00Added an answer on May 30, 2026 at 5:17 am

    Here is a solution using find_if instead of fold:

    #include <type_traits>
    #include <boost/mpl/end.hpp>
    #include <boost/mpl/find_if.hpp>
    #include <boost/mpl/logical.hpp>
    
    template
    <
        typename Sequence,
        typename Pred
    >
    struct all_of
    :
        std::is_same< typename
            boost::mpl::end< Sequence >::type, typename
            boost::mpl::find_if<
                Sequence,
                boost::mpl::not_< Pred >
            >::type
        >
    {};
    

    If you want to stick to fold, you’ll need to turn the predicate into a metafunction using lambda before invoking it, to protect the arguments:

    boost::mpl::apply< typename
        boost::mpl::lambda< Pred >::type,
        boost::mpl::_2
    >
    

    However, you’ll note that this won’t work either. I’m not sure why exactly, I think it is related to this discussion on the Boost mailing list. Apparently, there is an issue with the arity of apply which is greater than the one supported by lambda. Anyway, a simple workaround is to use apply1 instead of apply. Here is the full solution:

    template
    <
            typename Sequence,
            typename Pred
    >
    struct all_of
      : boost::mpl::fold<
            Sequence,
            boost::mpl::true_,
            boost::mpl::and_<
                boost::mpl::_1,
                boost::mpl::apply1< typename
                    boost::mpl::lambda< Pred >::type,
                    boost::mpl::_2
                >
            >
        >
    {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following function template: template<typename T> void Foo(T) { // ... } Pass-by-value
Consider the following code: require(Hmisc) num.boots <- 10 data <- rchisq(500, df = 5)
I've got some basic questions about C++. Consider the following code in which I
Consider the following piece of code: final Foo foo = context.mock(Foo.class); context.checking(new Expectations() {{
consider the following scenario: I have a storyboard-based app. I add a ViewController object
Consider the following code, where each key has an identical value: IDictionary<string, string> quarterbackDictionary
Background Consider the following input: <Foo Bar=bar Baz=1 Bax=bax > After processing, I need
Ok this looks like a major fundamental bug in .NET: Consider the following simple
Consider the following Entities. public class Product{ int id; Date effectiveDate; Date expiryDate; Set<Inventory>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.