c++ - Nested class declaration: template vs non-template outer class -


i have c++ template class has nested class inside, like:

template<int d> class outer_t { public:     class inner;      inner i; };  template<int d> class outer_t<d>::inner { public:     float x; };  int main () {     outer_t<3> o_t; // 3 or arbitrary int     o_t.i.x = 1.0;    return 0; } 

this compiles without problems. however, declare similar non-template class, this:

class outer_1 { public:     class inner;      inner i; };  class outer_1::inner { public:     float x; };  int main () {     outer_1 o1;     o1.i.x = 1.0;    return 0; } 

i start getting following error (i'm using gcc 4.6.3): "error: field ‘i’ has incomplete type". know can remedy defining inner class inline, inside outer class:

class outer_2 { public:     class inner {     public:         float x;     };      inner i; }; 

this compile, avoid defining nested class inline. have 2 questions: reason apparently odd discrepancy between template , non-template nested class declaration? , there elegant way declare , use nester class inside outer class, while avoiding defining inline, in same style template class? in advance help!

why compilation error?

the compiler needs know size of member variable types in order object layout. outer_1, because inner forward declaration, don't know size. hence compilation error. outer_2 however, definition inline, know size of time member variable of type inner.

why templates pass?

class templates, on other hand, don't defined until template instantiation occurs. in example, implicit instantiation occurs in main. @ point, definition of inner complete , therefore size known. can see case using explicit instantiation before definition of inner.

 template<int d>  class outer_t  {  public:      class inner;       inner i;  };   template class outer_t<3>;  // explicit instantation   template<int d>  class outer_t<d>::inner  {  public:      float x;  };   int main ()  {      outer_t<3> o_t; // 3 or arbitrary int      o_t.i.x = 1.0;     return 0;  } 

clang produces following error:

 a.cc:7:11: error: implicit instantiation of undefined member 'outer_t<3>::inner'      inner i;            ^  a.cc:10:16: note: in instantiation of template class 'outer_t<3>' requested here  template class outer_t<3>;            ^  a.cc:5:11: note: member declared here      class inner;            ^ 

the solution

if want pull out definition of nested class, suggested solution have templatized outer_t, provide alias convenience.

 template <typename dummy = void>  class outer_1_impl {    public:     class inner;     inner i;  };   template <typename dummy>  class outer_1_impl<dummy>::inner {    public:    float x;  };   using outer_1 = outer_1_impl<>;   int main () {    outer_1 o1;    o1.i.x = 1.0;  } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -