Skip to main content
gamedev.net
🔒 Locked

Template Specialization

Started by _goat 0 replies 1k views
Original Post
_goat
_goat
This hurts my head. The following works like a charm:
#include <iostream>

template <typename T> struct A;
template <typename T> struct B;

typedef A<int> AA;
typedef B<int> BB;

template <> struct B<int>
{
	void eat()
	{
		AA::do_nothing();
		std::cout << "EATING" << std::endl;
	}
};

template <> struct A<int>
{
	void do_thing()
	{
		BB b;
		b.eat();
	}
	
	static void do_nothing()
	{
	}
};

struct ESS
{
	ESS()
	{
	}
	
	void d()
	{
		int a = 1, b = 2, c = a + b;
	}
};




int main()
{
	// just to make sure it's not optimised away
	ESS es; 
	es.d();
	
	// do a thing
	AA a; 
	a.do_thing();
}
However, if I move the whole ESS part in between the specialisations, it does not work:
#include <iostream>

template <typename T> struct A;
template <typename T> struct B;

typedef A<int> AA;
typedef B<int> BB;

template <> struct B<int>
{
	void eat()
	{
		AA::do_nothing();
		std::cout << "EATING" << std::endl;
	}
};

// ESS is now in between
struct ESS
{
	ESS()
	{
	}
	
	void d()
	{
		int a = 1, b = 2, c = a + b;
	}
};

template <> struct A<int>
{
	void do_thing()
	{
		BB b;
		b.eat();
	}
	
	static void do_nothing()
	{
	}
};



int main()
{
	// just to make sure it's not optimised away
	ESS es; 
	es.d();
	
	// do a thing
	AA a; 
	a.do_thing();
}
And if I have it anywhere above the specialisation of B, then it compiles again (not going to show this one :D). For the middle case, where it doesn't work, the error is:
main.cpp(19) : error C2027: use of undefined type 'A<T>'
        with
        [
            T=int
        ]
main.cpp(19) : error C3861: 'do_nothing': identifier not found
It is really doing my head in - does anyone know why that could be? I'm on MSVC2k5 if that helps.
joanusdmentia
joanusdmentia
Neither version works for me, and I didn't expect them to. You're trying to use the type A<int> before it's been defined, the fact that you're doing template specialisation really has nothing to do with the problem. What you need to do is separate the function definitions from the class definitions:

#include <iostream>template <typename T> struct A;template <typename T> struct B;typedef A<int> AA;typedef B<int> BB;template <> struct B<int>{	void eat();};template <> struct A<int>{	void do_thing();	static void do_nothing() {}};void B<int>::eat(){	AA::do_nothing();	std::cout << "EATING" << std::endl;}void A<int>::do_thing(){	BB b;	b.eat();}struct ESS{	ESS()	{	}		void d()	{		int a = 1, b = 2, c = a + b;	}};int main(){	// just to make sure it's not optimised away	ESS es; 	es.d();		// do a thing	AA a; 	a.do_thing();}


EDIT: I'm using VS2005 as well.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.