abraham_hilbert abraham_hilbert - 1 month ago 12
C++ Question

class instantiation in nested classes

I have the following problem:

template< typename T, size_t N, size_t... N_i >
struct A
{
// nested class
template< typename... Ts >
class B
{
//...

A<T, N_i...>::B< Ts... > operator[]( size_t i )
{
A< T, N_i...>::B< Ts... > res{ /* ... */ };

return res;
}

// ...
};
};


Unfortunately, the compiler yields an error for "
A < T, N_i...>::B < Ts... > res{ /* ... */ };
". Does anyone know how I can return an instantiation of the
class B
(which differs in its template arguments of his outer
class A
) in the function "
operator[]
" of
class B
?

Many thanks in advance.

Answer

Should be enough add a typename before A<T, N_i...> and a template before B< Ts... >.

I mean

template< typename T, size_t N, size_t... N_i >
struct A
{
  // nested class
  template< typename... Ts >
  class B
  {
      //...

      typename A<T, N_i...>::template B< Ts... > operator[]( size_t i )
      {
        typename A< T, N_i...>::template B< Ts... > res{ /* ... */ };

        return res;
      }

      // ...
  };
}

P.s.: should work also with C++11; not only with C++14