Last active
January 1, 2016 04:48
-
-
Save wolf0403/8093855 to your computer and use it in GitHub Desktop.
How to define static member functions of class templates correctly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "a.h" | |
int main(void) | |
{ | |
A<int> aInt; | |
A<float> aFloat; | |
A<int>::print(); | |
A<float>::print(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "a.h" | |
int main(void) | |
{ | |
A<int> aInt; | |
A<float> aFloat; | |
A<int>::print(); | |
A<float>::print(); | |
return 0; | |
} | |
rrmbp:cpp ryan$ cat b.cpp | |
#include "a.h" | |
rrmbp:cpp ryan$ cat a.h | |
#ifndef __A_H__ | |
#define __A_H__ | |
#include <iostream> | |
template <class T> | |
class A | |
{ | |
private: | |
static T counter; | |
public: | |
static void print(); | |
}; | |
#endif // __A_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "a.h" | |
int b_fun() { | |
A<int>::print(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "a.h" | |
template<> | |
void A<int>::print() {} | |
template<> | |
void A<float>::print() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a.out: a.o b.o def.o | |
%.o: %.cpp | |
clang++ -c -o $@ $< |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment