nested templates: legal?

I'm trying to have a templated function inside a template class. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T> class MyClass
{
  template <typename TT> T func(TT p);
};

//--------------------

template <typename T>
template <typename TT>
T MyClass<T>::func(TT p)
{
  // blah
}


This compiles fine in GCC, but is exploding in VS2002. Is this legal? Is there an alternative way to do this?
Last edited on
Hi, not sure if I am interpretting the meaning of the word nested properly.

However, this is how I did nested (as I understand the word) templates in Visual studio 2008.

1
2
3
4
5
6
7
8
9
10
11
12
template <class ElementOfCommutativeRingWithIdentity>
class Monomial{};

template <class ElementOfCommutativeRingWithIdentity,
template<typename RingElementMonomial> class TemplateMonomial>
class TemplatePolynomial: public HashedListBasicObjects <TemplateMonomial<ElementOfCommutativeRingWithIdentity> >
{};

template <class ElementOfCommutativeRingWithIdentity>
class Polynomial: public TemplatePolynomial<ElementOfCommutativeRingWithIdentity,Monomial>
{
};


Notice there is a difference in the use of <typename T> and <class T>. What it is exactly I don't know. I once landed on a link (which started somewhere from this forum) explaining that the syntax is so because they had problems making the compiler run efficiently if they used a more reasonable syntax.

PS. Sorry for the long class names. This is how I like them.
Last edited on
Yeah that is a nested template but not the kind I was referring to. Doh!

I just mean a templated function inside a template class.
1
2
3
4
5
6
7
8
9
10
11
12
template <typename TTT, template <typename  TT> class T> 
class MyClass
{
   T<TTT> func(TTT p);
};


template <typename TTT, template <typename  TT> class T> 
T<TTT> MyClass<TTT,T>::func(TTT p)
{
  // blah
}


This compiles to me (VS 2008).

Can you go more specific? Does T depend on TT? (like I have assumed here?)

Last edited on
Note:
1. This is valid:
template <typename TTT, template <typename TT> class T>
2. This is NOT valid
template <typename TTT, template <typename TT> typename T>

I just remembered that was a difference between class and typename
Last edited on
no no. See my original example.

Single argument templates. The class is a template, and the class has a member function that's also a template. Each template can be its own type (ie: MyClass<int>::myfunc<float>)

I'm not looking for a template argument that's a template.

I don't know how to be more specific. Words are failing me, apparently XD. I appreciate all the effort, though.


EDIT--

upon a bit more testing... implicitly inlining the function works fine:

1
2
3
4
5
6
7
8
9
10
11
12
// This code compiles fine in GCC and VS
template <typename T>
class C
{
public:
    template <typename TT>
    T func(TT v)
    {
        cout << "called\n";
        return 0;
    }
};


But how would I go about moving 'func's body so it's not implicitly inlined?
Last edited on
I finally understood!

Well, your original code compiles under Visual studio 2008 Express just fine. Perhaps your compiler is a bit outdated?
This compiled fine on MinGW, for me:
1
2
3
4
5
6
7
8
9
10
11
template <typename T1>
struct A{
	template <typename T2>
	T1 f(T2 a);
};

template <typename T1>
template <typename T2>
T1 A<T1>::f(T2 a){
	return *this;
}
Did you experiment with this syntax template <typename TT,typename T> instead of template <typename T1> template <typename T2>?
P.S. all of the suggestions so far compile on my VS...
Last edited on
Perhaps your compiler is a bit outdated?


Alright that's what I figured. I'm fine if that was the case.

I would update my VS if I thought I could without updating my OS (my windows box still has Win2k and I don't feel like shelling out a few hundred to upgrade to an OS I'll hardly ever use)

I'm having a few more template related errors (like it failing to instantiate templates when there's no reason it shouldn't be able to). I'm wondering if those are just because it's outdated as well.

Bah.

Anyway thanks for the help. I suppose I'll try updating VS on this comp. If it works, then yay.
Topic archived. No new replies allowed.