Nested class declaration: template vs non-template external class

I have a C ++ template class that has a nested class inside, for example:

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 any arbitrary int
    o_t.i.x = 1.0;

  return 0;
}

This compiles without any problems. However, as soon as I declare a similar class without a template, something like 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 am starting to get the following error (I am using gcc 4.6.3): “error: field” I have an incomplete type. ”I know that I can fix this by simply defining the inline inner class inside the outer class:

class Outer_2
{
public:
    class Inner {
    public:
        float x;
    };

    Inner i;
};

, inline. , : , -, - ? nester , , , ? !

+4
2

?

-, . Outer_1, Inner , . . Outer_2 , Inner.

?

, , , . main. Inner , , . , 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 any arbitrary int
     o_t.i.x = 1.0;

   return 0;
 }

Clang :

 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 is declared here
     class Inner;
           ^

, , , Outer_t, .

 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;
 }
+4

, . , , Outer_1::Inner , . Inner - , Outer_2.

? : Outer_t main(), Outer_t<T>::Inner!... Outer_t<T> T Outer_t .

+1

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


All Articles