Conditional compile-time inclusion/exclusion of code based on template argument(s)?

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

template
class X{
  template
  struct Y{};

  template
  struct Y{};
};

Now, this example will obviously not compile, with the error that the second X::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 

template
class X{
  template::type>
  struct Y{};

  template::type>
  struct Y{};
};

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

template
class X{
  __include_if(I == 1){
    template
    struct Y{};
  }

  __include_if(I == 2){
    template
    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. :/

25
задан Xeo 14 April 2011 в 07:54
поделиться