Last active
March 22, 2018 22:19
-
-
Save tuket/0902423431d31cc7b6896554d509db7a to your computer and use it in GitHub Desktop.
type_traits example
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 <iostream> | |
#include <type_traits> | |
template <typename T> | |
typename std::enable_if<std::is_arithmetic<T>::value, T>::type | |
squared(T t, bool b) | |
{ | |
return b ? t * t : t; | |
} | |
int main() | |
{ | |
std::cout << squared(5, true) << std::endl; | |
} |
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 <type_traits> | |
#include <string> | |
using namespace std; | |
template <typename T, typename Enable = void> | |
class Attrib | |
{ | |
T* t; | |
public: | |
T* operator->() { return t; } | |
}; | |
template <typename T> | |
class Attrib<typename T, typename enable_if<is_arithmetic<T>::value | is_same<T, string>::value>::type> | |
{ | |
T* t; | |
public: | |
Attrib(T& t) { this->t = &t; } | |
T* operator->() { return t; } | |
}; | |
/*template <typename T> | |
class Attrib<typename T, typename enable_if<is_same<T, string>::value>::type> | |
{ | |
T* t; | |
public: | |
Attrib(T& t) { this->t = &t; } | |
T* operator->() { return t; } | |
};*/ | |
int main() | |
{ | |
int a; | |
Attrib<int> A(a); | |
char c; | |
Attrib<char> C(c); | |
string s; | |
Attrib<string> S(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1