Class Template Inside Of Class Template

How do you declare a templated class inside of another templated class in such as that you can use it like this (where b is a templated class inside of class template a).

1
2
3
4
5
6
7
8
9
10
11
int const arg1=100;
int const arg2=101;


a< arg1, arg2 >::b< /*typename such as*/ char > pvar;
a< arg1, arg2 >::b xzar;

//what I mean by this is that b is a class, not a struct, so that operators
//on it can be overloaded
a< arg1, arg2 >::b jack=( xzar^(pvar*xzar), pvar );
Last edited on
Don't know why you need this, and certainly have no idea why macros are required; but this is the syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

template < int X, int Y > struct outer
{
    template < typename T > struct inner
    {
        T matrix[X][Y] ;
        static constexpr int bytes = X*Y * sizeof(T) ;
        template < typename U > using rebind = typename outer<X,Y>::template inner<U> ;
        template < int XX, int YY > using redim = typename outer<XX,YY>::template inner<T> ;
    };
};

template < int X, int Y, typename T > using inner_t = typename outer<X,Y>::template inner<T> ;

int main()
{
    outer<2,3>::inner<int> var { { { 0, 1, 2 }, { 3, 4, 5 } } } ;
    inner_t<2,3,int> cpy = var ;

    decltype(cpy)::rebind<char> another {};
    decltype(cpy)::redim<4,6> yet_another {};
    decltype(cpy)::redim<10,20>::rebind<char> a_third {};

    std::cout << var.bytes << ' ' << another.bytes << ' ' << yet_another.bytes << ' ' << a_third.bytes << '\n' ;
    static_assert( outer<1,1>::inner<char>::rebind<int>::redim<2,3>::bytes == sizeof(var), "must be equal" ) ;         
}

http://coliru.stacked-crooked.com/a/bdb07d1fcacdad7d
http://rextester.com/VMN81229
Thank You so much! You are incredibly helpful. Also, extra thanks for those links.

But... could you pretty please adjust your example to show how it could be implemented with b as a class instead of a struct. Thank you so much.
Last edited on
The only difference between the keywords struct and class is in the default (member and base class) access.

1
2
3
4
5
6
7
8
9
10
11
12
template < int X, int Y > class outer
{
    public:
        template < typename T > class inner
        {
            public:
                T matrix[X][Y] ;
                static constexpr int bytes = X*Y * sizeof(T) ;
                template < typename U > using rebind = typename outer<X,Y>::template inner<U> ;
                template < int XX, int YY > using redim = typename outer<XX,YY>::template inner<T> ;
        };
};

http://coliru.stacked-crooked.com/a/7361d4aeeb431218
Thank You so so very incredibly much, you are so incredibly helpful! So, THANK YOU!!! But, only just but 1 more question persists: how do I implement it with unions like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

template < const int X, const int Y > class outer
{
    public:
        template < typename T > class inner
        {
            public:
                T matrix[X][Y] ;
                static constexpr int bytes = X*Y * sizeof(T) ;
                template < typename U > using rebind = typename outer<X,Y>::template inner<U> ;
                template < int XX, int YY > using redim = typename outer<XX,YY>::template inner<T> ;
        };
};

template <const int XX, const int YY, typename TYPE>
union myunion {
	char cr[8];
	unsigned long long ull;
	outer<XX,YY>::inner<TYPE> customclass;
};

int main( void ) {
	
	myunion<2,3,int> myvar;
	
	cout << myvar.customclass.bytes;
	
	return 0;
}
Last edited on
1
2
3
4
5
6
7
template < int XX, int YY, typename TYPE > // const is redundant; 
                                           // template non-type parameters are (literal) constants
union myunion {
	char cr[8];
	unsigned long long ull;
	typename outer<XX,YY>::template inner<TYPE> customclass;
};


typename : disambiguate that the dependant name is the name of a type
See: http://www.cplusplus.com/forum/beginner/103508/#msg557627

template : disambiguate that the dependant name is the name of a template.
in a template definition, a dependent name that is not a member of the current instantiation is not considered to be a template name unless the disambiguation keyword template is used or unless it was already established as a template name.
'The template disambiguator for dependent names' http://en.cppreference.com/w/cpp/language/dependent_name


Consider using a union-like-class.
See: 'Union-like classes' http://en.cppreference.com/w/cpp/language/union
Thank you so incredibly much, that is all, thank you!
Last edited on
Topic archived. No new replies allowed.