Skip to content

Instantly share code, notes, and snippets.

@wolf0403
Last active January 1, 2016 04:48
Show Gist options
  • Save wolf0403/8093855 to your computer and use it in GitHub Desktop.
Save wolf0403/8093855 to your computer and use it in GitHub Desktop.
How to define static member functions of class templates correctly.
#include "a.h"
int main(void)
{
A<int> aInt;
A<float> aFloat;
A<int>::print();
A<float>::print();
return 0;
}
#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__
#include "a.h"
int b_fun() {
A<int>::print();
}
#include "a.h"
template<>
void A<int>::print() {}
template<>
void A<float>::print() {}
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