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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:44:51+00:00 2026-06-18T03:44:51+00:00

A piece of code is worth a thousand words: int main() { // All

  • 0

A piece of code is worth a thousand words:

int main()
{
    // All of the following calls return true:
    AreEqual(1, 1);
    AreEqual(1, 1, 1);
    AreEqual(1, 1, 1, 1);
    AreEqual(1, 1, 1, 1, 1);

    // All of the following calls return false:
    AreEqual(1, 2);
    AreEqual(1, 2, 1);
    AreEqual(1, 7, 3, 1);
    AreEqual(1, 4, 1, 1, 1);    
}

How to implement the function AreEqual() that accepts arbitrary number of arguments?

The trivial but tedious soultion is through overloading:

bool AreEqual(int v1, int v2);
bool AreEqual(int v1, int v2, int v3);
bool AreEqual(int v1, int v2, int v3, int v4);
......

Another trivial but not workable solution is:

bool AreEqual(...);

This solution is not workable, because the caller must add another argument (argument count or ending marker) to specify the number of the arguments.

Yet another way is through variadic template arguments

template<class... Args>
bool AreEqual(Args... args)
{
    // What should be placed here ???
}
  • 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-18T03:44:53+00:00Added an answer on June 18, 2026 at 3:44 am

    Here is how one can implement it with templates:

    #include <iostream>
    #include <iomanip>
    
    template<class T0>
    bool AreEqual(T0 t0) { return true; }
    
    template<class T0, class T1, class... Args>
    bool AreEqual(T0 t0, T1 t1, Args ... args) {
      return t0 == t1 && AreEqual(t1, args...);
    }
    
    
    int main () {
      std::cout << std::boolalpha;
    
        // All of the following calls return true:
      std::cout<< AreEqual(1, 1) << "\n";
      std::cout<< AreEqual(1, 1, 1) << "\n";
      std::cout<< AreEqual(1, 1, 1, 1) << "\n";
      std::cout<< AreEqual(1, 1, 1, 1, 1) << "\n\n";
    
        // All of the following calls return false:
      std::cout<< AreEqual(1, 2) << "\n";
      std::cout<< AreEqual(1, 2, 1) << "\n";
      std::cout<< AreEqual(1, 7, 3, 1) << "\n";
      std::cout<< AreEqual(1, 4, 1, 1, 1)  << "\n";
    }
    

    You should consider whether these variations are appropriate for your use:

    • Use references instead of pass-by-value
    • Make the non-variadic template take two parameters instead of one.

    Alternatively, here is a non-recursive version. Sadly, it does not short-curcuit. To see a non-recursive short-circuiting version see the other answer.

    template<typename T, typename... Args>
    bool AreEqual(T first, Args... args)
    {
      return std::min({first==args...});
    }
    

    Finally, this version is attractive in its lack of subtlety:

    template<typename T, typename... Args>
    bool AreEqual(T first, Args... args)
    {
      for(auto i : {args...})
        if(first != i)
          return false;
      return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A piece of code is worth a thousands words. #include <iostream> #include <type_traits> using
Is it worth to write this piece of code: RelayCommand _saveCommand; public ICommand SaveCommand
a piece of code here jmp_buf mark; int Sub_Func() { int be_modify, jmpret; be_modify
Given the following piece of code: using(var data = new SomeDataContext(ConnectionString)) { data.ObjectTrackingEnabled =
I'd like to optimize this piece of code : public void PopulatePixelValueMatrices(GenericImage image,int Width,
Saw piece of code in book: T& operator[](int i) throw(RangeError) { if(i >= 0
Description: Following code piece is used in Android/iPhone for Roundabout carousal. Each LI is
my piece of code is: for(int i=0;i<list.size();i++){ listRetweeters = null; tweetId = list.get(i).getId(); status
This piece of code pulls off all the images from your web page and
First piece of code: // code is a private global variable for the class

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.