Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Templated Functions

This topic is 5763 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

If I include the following header file
template <typename Z> class bar {
	public :
		Z a;

	bar(Z b) : a(b) { }
	~bar() {}

template <typename Z> bar<Z> ram(Z a) {
	return bar<Z>(a);

bar<int> ewe(int a) {
	return bar<int>(a);
in seperate source files the linker naturally complains about the multiply defined symbol ewe. However it doesn''t complain about the templated function ram. Why is this exactly?

Share this post

Link to post
Share on other sites
Because ram doesn''t ''exist'' (as far as the linker is concerned) until it is used and given a type for Z.
Template functions are molds to make functions from - instructions for the compiler which constructs a new function for each type you give it.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
I''m pretty sure that...
(not positive)
templates are not actual code until they are used somewhere, ie they are instianited (sp?). In fact, as I remember, in the Standard, I don''t think the template is even supposed to be compiled
So, because Ewe isn''t a template function, then it _is_ defined, and multiple defines are obviously not allowed..
however for Ram, it doesn''t exist until it is used, so you could indeed include it many many times

I think.

(so I''m too lazy to change my password so I can log in again)

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!