Last active
July 25, 2016 05:31
-
-
Save qgp9/68a7bb015f278d9da838e702741ff2e7 to your computer and use it in GitHub Desktop.
How to reduce duplications of code while template partial specialization
This file contains 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<iostream> | |
using namespace std; | |
template<class T, int I> | |
class MyClassB; | |
// HELPER | |
template<class T, int I> | |
struct MyClassBImpl{ | |
// You may be able to get a pointer of MyClassBImpl as a parameter | |
static void Impl(){ | |
cout<<"Constructor of MyClassBImpl : default"<<endl; | |
} | |
static void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : default"<<endl; | |
} | |
}; | |
template<class T> | |
struct MyClassBImpl<T,1> { | |
static void Impl(){ | |
cout<<"Constructor of MyClassBImpl : I==1"<<endl; | |
} | |
static void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : I==1"<<endl; | |
} | |
}; | |
// TEMPLATE | |
template<class T, int I> | |
class MyClassB { | |
public: | |
MyClassB(){ MyClassBImpl<T,I>::Impl(); } | |
void doSomething(){ | |
MyClassBImpl<T,I>::doSomething(); | |
}; | |
}; | |
// TEST | |
int main(){ | |
MyClassB<int, 1> a1; | |
MyClassB<int, 2> a2; | |
MyClassB<int, 3> a3; | |
a1.doSomething(); | |
a2.doSomething(); | |
a3.doSomething(); | |
} | |
This file contains 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<iostream> | |
using namespace std; | |
template<class T, int I> | |
class MyClassB; | |
// HELPER | |
template<class T, int I> | |
struct MyClassBImpl{ | |
MyClassBImpl(){ | |
cout<<"Constructor of MyClassBImpl : default"<<endl; | |
} | |
void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : default"<<endl; | |
} | |
}; | |
template<class T> | |
struct MyClassBImpl<T,1> { | |
MyClassBImpl(){ | |
cout<<"Constructor of MyClassBImpl : I==1"<<endl; | |
} | |
void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : I==1"<<endl; | |
} | |
}; | |
// TEMPLATE | |
template<class T, int I> | |
class MyClassB { | |
public: | |
MyClassB():fImpl(){} | |
void doSomething(){ | |
fImpl.doSomething(); | |
}; | |
private: | |
MyClassBImpl<T,I> fImpl; | |
}; | |
// TEST | |
int main(){ | |
MyClassB<int, 1> a1; | |
MyClassB<int, 2> a2; | |
MyClassB<int, 3> a3; | |
a1.doSomething(); | |
a2.doSomething(); | |
a3.doSomething(); | |
} |
This file contains 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<iostream> | |
using namespace std; | |
template<class T, int I> | |
class MyClassB; | |
// HELPER | |
template<class T, int I> | |
class MyClassBImpl{ | |
public: | |
MyClassBImpl( MyClassB<T,I> *m):fMother(m){ | |
cout<<"Constructor of MyClassBImpl : default"<<endl; | |
} | |
void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : default"<<endl; | |
} | |
private: | |
MyClassB<T,I> *fMother; | |
}; | |
template<class T> | |
class MyClassBImpl<T,1> { | |
public: | |
MyClassBImpl( MyClassB<T,1> *m):fMother(m){ | |
cout<<"Constructor of MyClassBImpl : I==1"<<endl; | |
} | |
void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : I==1"<<endl; | |
} | |
private: | |
MyClassB<T,1> *fMother; | |
}; | |
// TEMPLATE | |
template<class T, int I> | |
class MyClassB { | |
public: | |
MyClassB():fImpl(this){} | |
void doSomething(){ | |
fImpl.doSomething(); | |
}; | |
private: | |
MyClassBImpl<T,I> fImpl; | |
}; | |
// TEST | |
int main(){ | |
MyClassB<int, 1> a1; | |
MyClassB<int, 2> a2; | |
MyClassB<int, 3> a3; | |
a1.doSomething(); | |
a2.doSomething(); | |
a3.doSomething(); | |
} | |
This file contains 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<iostream> | |
using namespace std; | |
template<class T, int I> | |
class MyClassB; | |
// Base Template Class | |
template<class T, int I> | |
class MyClassB { | |
public: | |
MyClassB(){} | |
virtual void doSomething(){} | |
}; | |
// Inherited Implementation | |
template<class T, int I> | |
class MyClassBImpl : public MyClassB<T,I>{ | |
public: | |
MyClassBImpl():MyClassB<T,I>(){ | |
cout<<"Constructor of MyClassBImpl : default"<<endl; | |
} | |
virtual void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : default"<<endl; | |
} | |
}; | |
template<class T> | |
class MyClassBImpl<T,1> : public MyClassB<T,1> { | |
public: | |
MyClassBImpl():MyClassB<T,1>(){ | |
cout<<"Constructor of MyClassBImpl : I==1"<<endl; | |
} | |
virtual void doSomething(){ | |
cout<<"doSomething in MyClassBImpl : I==1"<<endl; | |
} | |
}; | |
// TEST | |
int main(){ | |
MyClassBImpl<int, 1> a1; | |
MyClassBImpl<int, 2> a2; | |
MyClassBImpl<int, 3> a3; | |
a1.doSomething(); | |
a2.doSomething(); | |
a3.doSomething(); | |
} | |
This file contains 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
Constructor of MyClassBImpl : I==1 | |
Constructor of MyClassBImpl : default | |
Constructor of MyClassBImpl : default | |
doSomething in MyClassBImpl : I==1 | |
doSomething in MyClassBImpl : default | |
doSomething in MyClassBImpl : default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment