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

  • Home
  • SEARCH
  • 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 6738287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:20:58+00:00 2026-05-26T11:20:58+00:00

Having this code: template<class …Args> struct Are_Same { enum {value = Are_Same<Args…>::value}; }; template<class

  • 0

Having this code:

template<class ...Args>
struct Are_Same
{
    enum {value = Are_Same<Args...>::value};
};

template<class A,class... C>
struct Are_Same<A,C...>
{
    enum {value = Are_Same<A,C...>::value};//HERE is THE ERROREOUS LINE
};

template<class A,class B>
struct Are_Same<A,B>
{
    enum {value = std::is_same<A,B>::value};
};  

I’m getting error from gcc 4.6.1:

error: incomplete type ‘Are_Same’ used in
nested name specifier.

I thought that by doing Are_Same<A,C...>::value I will invoke recursive call which at the end will simply expand to Are_Same<A,B>. Obviously it’s not the case. Anyone knows where am I making mistake?

  • 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-26T11:20:58+00:00Added an answer on May 26, 2026 at 11:20 am

    I think that the definitions of the templates are wrong, in both cases you are triggering exact recursion. I would have expected the compiler to die with some stackoverflow inside the compiler but a different error is produced…

    An implementation of the are_same variadic template could be:

    template <class... Args>                    // base (optional to declare the template)
    struct are_same;
    
    template <class A, class B, class... Args>  // recursion
    struct are_same<A,B,Args...> {
        static const bool value = is_same<A,B>::value && are_same<A,Args...>::value;
    };
    
    template <class A, class B>                 // stop condition
    struct are_same<A,B> {
        static const bool value = is_same<A,B>::value;
    };
    

    Note that in the recursion step, one argument is dropped from the list of arguments, so that the new problem to resolve is a reduced version of the original. This type of template metaprogramming is quite related to recursion, and the same rules apply, to be able to use recursion you need to ensure that each recursive step gets you closer to a solution. In this particular case, given a list of N potentially same types, each step reduces the problem to finding whether N-1 types are the same.

    You can use alternatively, as stop condition (replacing the former one) a degenerate version of the are_same problem:

    template <class A> 
    struct are_same<A> {
       static const bool value = true;
    };
    

    Which is degenerate in the sense that it does not really make sense asking whether a single type *are_same*, but for different metaprogramming tasks it could be appropriate.

    A different potentially more efficient algorithm (I am not sure whether the compiler will avoid the instantiation of the template in the recursion step above) that does not depend on is_same could be:

    template <class... Args>
    struct are_same;
    
    template <class A, class... Args>
    struct are_same<A,A,Args...> {              // recursion
        static const bool value = are_same<A,Args...>::value;
    };
    
    template <class A, class B, class... Args>
    struct are_same<A,B,Args...> {              // cut, A and B are not the same
        static const bool value = false;
    };
    
    template <class A>
    struct are_same<A> {                        // end of recursion
        static const bool value = true;
    };
    

    In this case, the compiler will prefer the recursion to the cut steps whenever the two types are the same, so we need not check is_same internally. At the same time, if the compiler goes into the cut step, we don’t need to process the rest of the type list, as we already know the answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When declaring a template, I am used to having this kind of code: template
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
I'm having a hard time using std::string::iterators in C++. This code compiles fine (still
If I have this kind of hierarchy: #include <iostream> using namespace std; template<class T>
Having this code... var b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
I'm having trouble with this code: NSRect itemFrame; id item; // code to assign
I've been having problems with this code I had spent the last 3 hours
I am having some trouble with this code . The problem is when i
as all you know $(#ID) returns the element having ID. but this code always
I am having difficulties with forms, specifically ModelMultipleChoiceField. I've pieced together this code from

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.