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 8360827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:33:29+00:00 2026-06-09T11:33:29+00:00

Can you use C++11 variadic templates to complete /* ??? */ in: template<bool…v> struct

  • 0

Can you use C++11 variadic templates to complete /* ??? */ in:

template<bool...v> struct var_and { static bool constexpr value = /* ??? */; };

so that var_and<v...>::value provides && over the boolean pack v at compile-time?

Can you do the same for struct var_or<v...> for ||?

Can you use short-circuit evaluation (in both cases)?

Edit: An update to the accepted answer added that C++17 fold expressions enable

template<bool... v> constexpr bool var_and = (v && ...);
template<bool... v> constexpr bool var_or  = (v || ...);

It seems that, for parameter pack-based approaches, only a restricted type of “short-circuit evaluation” is possible: while instantiating var_or<true,foo(),bar()> only calls || once, it also calls both foo and bar.

  • 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-06-09T11:33:30+00:00Added an answer on June 9, 2026 at 11:33 am

    You don’t want value to be a typedef.

    template<bool head, bool... tail>
    struct var_and {
        static constexpr bool value = head && var_and<tail...>::value;
    };
    
    template<bool b> struct var_and<b> {
        static constexpr bool value = b;
    };
    

    Obviously the same can be done for ||.

    Short circuit evaluation doesn’t matter because this only deals with constant expressions which won’t have any side effects.

    Here’s another method which stops recursively generating types as soon as it find a false value, emulating a kind of short circuiting:

    template<bool head, bool... tail>
    struct var_and { static constexpr bool value = false; };
    
    template<bool... tail> struct var_and<true,tail...> {
        static constexpr bool value = var_and<tail...>::value;
    };
    
    template<> struct var_and<true> {
        static constexpr bool value = true;
    };
    

    Update for C++17: Using a fold expression makes this much simpler.

    template<bool...v> struct var_and {
        static constexpr bool value = (v && ...);
    };
    

    Or also using a template variable as enobayram suggests:

    template<bool... b> constexpr bool var_and = (b && ...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I use variadic templates without using the template parameters as function parameters? When
can use static methods/ classes safely in WCF due to the fact that WCF
I would like to use C++11's variadic templates to achieve a generalized random picker
I don't know if this can be achieved through variadic template, variadic marcos or
I'm looking for a way so that I can use the same portion of
I have a function that takes variadic arguments. These arguments are parameter-value pairs, so
A hypothetical variadic template tuple class would, as far as I can tell, have
In Java, we can use variadic function in the following way: public Set packStrings(String...strings){
I was looking at How to properly use references with variadic templates , and
I can use the following to get the contents of a folder with no

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.