Skip to content

Instantly share code, notes, and snippets.

@tuket
Last active March 22, 2018 22:19
Show Gist options
  • Save tuket/0902423431d31cc7b6896554d509db7a to your computer and use it in GitHub Desktop.
Save tuket/0902423431d31cc7b6896554d509db7a to your computer and use it in GitHub Desktop.
type_traits example
#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;
}
#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);
}
@tuket
Copy link
Author

tuket commented Mar 10, 2018

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment