Jump to content
  • Advertisement
Sign in to follow this  
Hobitz

C++ Function templates

This topic is 4148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i have a bit of problem about how to define member function template in source file. Here, for example:
//class.h
class A
{
public:
	template<class T>
		void Function(T& t);
};

//class.cpp
template<class T>
void A::Function(T& t)
{ }

//and when i try to use it
A a;
int i=1;
a.Function(i);

compiler says that A::Function<int> is unesolved external. What is a proper way to do that. Thanks

Share this post


Link to post
Share on other sites
Advertisement
On most compilers (except, IIRC, comeau, which implements the export keyword correctly), templatized function definitions must be included in same file with the function declaration (in this case class.h), or the header must #include the .cpp with the implementations (in which case, you should either exclude the .cpp from the build, or rename the extension to something like .impl to prevent the compiler from trying to compile it).

Share this post


Link to post
Share on other sites
Oh, i see. Then i will write my stuff directly in .h file. Thanks for quick answer.

Share this post


Link to post
Share on other sites
The problem is because the compiler needs acess to the template definition whenever you instantiate it so that it can:
1) check the syntax etc to ensure its valid
2) generate code for that template paramater.

If the template is only going to be used with a small number of types then you can explicitly instantiate it in the source file for each of those types (for example by taking the adress of the function) and only provide the decleration in the header which would cause the linker to resolve the references in other instantiations to the explicit instantiations but only for the types that it was explicitly instantiated with. This can reduce code bloat but IMHO it negates the usefulness of templates if your only going to use it with a small number of types I would put non-template overloads in the header and have the overloads call a template'ed implementation which only exists in the source file.

For example your code would become:

//class.h
struct A
{
template<class T>
void Function(T& t);
};

//class.cpp
template<class T>
void A::Function(T& t)
{ }

void (A::*Function_Instantiation_Int)(int&) = &A::Function<int>;

// and when you try to use it
A a;
int i=1;
a.Function(i); // resolves to the explicit instantiotion in class.cpp
float f = 0.0f;
a.Function(f); // unresolved external because theres no explicit instantiation in class.cpp



Quote:
On most compilers (except, IIRC, comeau, which implements the export keyword correctly), templatized function definitions must be included in same file with the function declaration.


Its EDG that implements export. ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!