Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring an instance of an explicit specializtion of a template within a regular class

I can't get this to compile at all. I may not be possible but I don't know why it should not be.

class A {
  template <typename T> 
  class B {
   int test() { return 0; }
  };
  //template <> class B<int>; <-with this, namepace error
  B<int> myB_;
 };

 template <> class A::B<int> {
  int test() {
   return 1;
  }
 };

As it appears the compiler complains "The explicit specialization "class A::B" must be declared before it is used." If I try to provide the froward declaration in the commented line, the compiler complains "The explicit specialization "B" must be declared in the namespace containing the template." We use 2 different compilers here. This error is from IBM's "xl" compiler on AIX but I get similar errors with different verbage when compiling on our Sun systems. It seems like a catch-22.

Obviously, this is a highly contrived, simplistic example but, it represents the problem. I want to define a template class within a class because the template class is only relevent to the containing class. There should be no access to the template from outside the class.

Am I missing something?

like image 451
user438938 Avatar asked Feb 02 '26 14:02

user438938


1 Answers

You are correct. This is not possible to do (as far as I know). Your member declaration causes an implicit instantiation before the explicit specialization was declared. But how would you want to declare it? You cannot do that in class scope. Others have felt that this is an ugly restriction.

You could work around this by making the class member a pointer. This would not need to implicitly instantiate the class at that point, but rather at the point where you create the object in the end. I realize that this is an ugly work around. So better find other ways to do this.

For instance partial specializations are allowed in class scope. So you could add a dummy template parameter, then you can specialize this in the class before the member declaration. Likewise, i find this ugly, but it would not disturb things that much, I feel.

like image 167
Johannes Schaub - litb Avatar answered Feb 05 '26 03:02

Johannes Schaub - litb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!