Jump to content
  • Advertisement
Sign in to follow this  
maxest

[C++] templates function specialization

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

I have some class with a function I want to be specialized. Just take a look:

#ifndef TEST
#define TEST

#include <iostream>

using namespace std;



template <class Type>
class Test
{
public:
void test()
{
printf("test1::general\n");
}
};

template<>
void Test<int>::test()
{
printf("test::int\n");
}




#endif

This code works pretty much as expected. If Test<int> is a type for a variable, then after calling test text "test::int" will show up.

But, I thought about moving that specialized test function to a cpp file, to speed the compilation time (in bigger project of course). So now I have:
header:

#ifndef TEST
#define TEST

#include <iostream>

using namespace std;



template <class Type>
class Test
{
public:
void test()
{
printf("test1::general\n");
}
};




#endif

source:

#include "test.h"



template<>
void Test<int>::test();



template<>
void Test<int>::test()
{
printf("test::int\n");
}

Now, after calling test of a Test<int> variable I get "test::general" text. Is there a way to make this work like the previous version?

Share this post


Link to post
Share on other sites
Advertisement

Could you please specify what you mean by:

explicitly instantiate a Test<int> in the .cpp file

?




all template code (function implementations for example) must go into header

Share this post


Link to post
Share on other sites
Write "template class Test<int>;" in the source file.

all template code (function implementations for example) must go into header


Firstly, sorry for the downvote, I missed the 'quote' button with my trackpad. Secondly, this isn't true: like I say, you can put the implementation in the source file so long as you explicitly instantiate it for all the types you need.

Share this post


Link to post
Share on other sites

Write "template class Test<int>;" in the source file.
[/quote]
Unfortunately it doesn't work. The problem is that I still have a "default" definition of function test in the header file. If I remove it, it is okay.
The point is that I want to have some default behaviour in all different classes that are generated. In this case I want all templates to print "test1::general" except for the specialized int template which should print "test1::int".

Share this post


Link to post
Share on other sites

[quote name='maxest' timestamp='1321476238' post='4884687']
Could you please specify what you mean by:

explicitly instantiate a Test<int> in the .cpp file

?


C++ FAQ: [35.12] Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?
C++ FAQ: [35.13] How can I avoid linker errors with my template functions?
[/quote]


http://msdn.microsoft.com/en-us/library/by56e477(v=vs.80).aspx

Share this post


Link to post
Share on other sites
Unfortunately it doesn't work.


It does.

test.h
[source lang="cpp"]#ifndef TEST_H
#define TEST_H

#include <iostream>

template <typename T>
struct Test
{
void test() { std::cout << "Test" << std::endl;
};

#endif[/source]

test.cpp
[source lang="cpp"]#include "test.h"

template <>
void Test<int>::test()
{ std::cout << "Special!" << std::endl; }

template struct Test<int>;[/source]

main.cpp
[source lang="cpp"]#include "test.h"

int main()
{
Test<int> t;
t.test(); // prints "Special!"
}[/source]

You may want to show actual code.

Share this post


Link to post
Share on other sites

Write "template class Test<int>;" in the source file.

[quote name='zfvesoljc' timestamp='1321477172' post='4884692']all template code (function implementations for example) must go into header


Firstly, sorry for the downvote, I missed the 'quote' button with my trackpad. Secondly, this isn't true: like I say, you can put the implementation in the source file so long as you explicitly instantiate it for all the types you need.
[/quote]

I stand corrected

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!