Declaring an instance of an explicit template specification in a regular class

I cannot get this to compile at all. Perhaps I cannot, but I do not know why this 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;
  }
 };

The compiler appears to be complaining: "The explicit specialization" class A :: B "must be declared before using it." If I try to provide a froward declaration in the comments, the compiler complains "Explicit specialization" B "must be declared in the namespace containing the template." Here we use 2 different compilers. This error is related to the IBM "xl" compiler on AIX, but when compiling on our Sun systems, similar errors occur with different options. Sounds like a trick-22.

, , , . , . .

- ?

+3
3

. ( ). . ? . , .

, . , , . , . .

, . , , . , , .

+3

, :

namespace {

  template <typename T> 
  class B {
   int test() { return 0; }
  };

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

}

class A {
  B<int> myB_;
};

, A , (, factory Pimpl).

0

B is not a template class, and you are trying to specialize it. This is the cause of the error. You can check these two errors C2913 and C3413 . Is this what you are looking for?

class A
{
   template<class T>
   class B
   {
      inline int test()
      {
         return 0;
      }
   };
   A::B<int> myB_;
};
0
source

Source: https://habr.com/ru/post/1763070/


All Articles