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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:41:51+00:00 2026-05-23T01:41:51+00:00

Consider the following class, with the inner struct Y being used as a type,

  • 0

Consider the following class, with the inner struct Y being used as a type, eg. in templates, later on:

template<int I>
class X{
  template<class T1>
  struct Y{};

  template<class T1, class T2>
  struct Y{};
};

Now, this example will obviously not compile, with the error that the second X<I>::Y has already been defined or that it has too many template parameters.
I’d like to resolve that without (extra) partial specialization, since the int I parameter isn’t the only one and the position of it can differ in different partial specializations (my actual struct looks more like this, the above is just for simplicity of the question), so I’d like one class fits every I solution.


My first thought was obviously enable_if, but that seems to fail on me, eg. I still get the same errors:

// assuming C++11 support, else use boost
#include <type_traits>

template<int I>
class X{
  template<class T1, class = std::enable_if<I==1>::type>
  struct Y{};

  template<class T1, class T2, class = std::enable_if<I==2>::type>
  struct Y{};
};

So, since enable_if fails, I hope there is another way to achieve the following compile time check:

template<int I>
class X{
  __include_if(I == 1){
    template<class T1>
    struct Y{};
  }

  __include_if(I == 2){
    template<class T1, class T2>
    struct Y{};
  }
};

It would just be to save me a lot of code duplication, but I’d be really happy if it was somehow possible.
Edit: Sadly, I can’t use the obvious: variadic templates, as I’m using Visual Studio 2010, so only the C++0x stuff that is supported there I can use. :/

  • 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-23T01:41:52+00:00Added an answer on May 23, 2026 at 1:41 am

    There are two problems here:

    1. enable_if works with partial specialization, not primary templates.
    2. The number of externally-visible arguments is determined by the primary template, of which there may be only one.

    Answer 1.

    As you suggested in chat, a linked list of templates can emulate the variadic parameter pack.

    template<int I>
    class X{
      template<class list, class = void>
      struct Y;
    
      template<class list>
      struct Y< list, typename std::enable_if<I==1>::type > {
          typedef typename list::type t1;
      };
    
      template<class list>
      struct Y< list, typename std::enable_if<I==2>::type > {
          typedef typename list::type t1;
          typedef typename list::next::type t2;
      };
    };
    

    If you end up with next::next::next garbage, it’s easy to write a metafunction, or use Boost MPL.


    Answer 2.

    The different-arity templates can be named similarly but still stay distinct if they are nested inside the SFINAE-controlled type.

    template<int I>
    class X{
      template<typename = void, typename = void>
      struct Z;
    
      template<typename v>
      struct Z< v, typename std::enable_if<I==1>::type > {
          template<class T1>
          struct Y{};
      };
    
      template<typename v>
      struct Z< v, typename std::enable_if<I==2>::type > {
          template<class T1, class T2>
          struct Y{};
      };
    };
    
    X<1>::Z<>::Y< int > a;
    X<2>::Z<>::Y< char, double > b;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: class Rectangle { public: // Constructors Rectangle(){ init(0,0); } Rectangle(int
Consider the following simplified version of my code. I have a template class A
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
Consider the following classes: class Coord { public: double _x, _y; Coord(double x, double
Consider the following code : class TextMessage{ public : TextMessage(){}; TextMessage(std::string _text):text(_text){} std::string text;
consider the following program: class Base { public: virtual void foo() const { cout
Consider the following code snippet below. class X { public String toString() { return
Consider the following (tested with Scala 2.8.1 and 2.9.0): trait Animal class Dog extends
I think I may have some confusion about the domain/service layer separation. In my
I need some advice here, I hope somebody can help me. I have the

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.