Created
September 20, 2023 09:34
-
-
Save scc-tw/bc546c4a71d0cd087658a3fa6b3ddce9 to your computer and use it in GitHub Desktop.
concepts_in_C++98
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.hpp" | |
template <typename T, typename U> | |
struct is_addable | |
{ | |
static const bool | |
value = | |
is_arithmetic<T>::value && is_arithmetic<U>::value; | |
}; | |
template <typename T, typename U> | |
typename enable_if<is_addable<T, U>::value, T>::type add(T a, U b) | |
{ | |
return a + b; | |
} | |
int main() | |
{ | |
const int a = 5; | |
const double b = 3.14; | |
const double res = add(a, b); | |
std::cout << "Result: " << res << std::endl; | |
// const char c[] = "A"; | |
// double res2 = add(a, c); // Uncomment this would get a compilation error | |
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
#ifndef __MY_TYPE_TRAITS_HPP__ | |
#define __MY_TYPE_TRAITS_HPP__ | |
template <typename T> | |
struct is_integral | |
{ | |
static const bool value = false; | |
}; | |
template <> | |
struct is_integral<int> | |
{ | |
static const bool value = true; | |
}; | |
template <> | |
struct is_integral<unsigned int> | |
{ | |
static const bool value = true; | |
}; | |
template <> | |
struct is_integral<long> | |
{ | |
static const bool value = true; | |
}; | |
template <> | |
struct is_integral<unsigned long> | |
{ | |
static const bool value = true; | |
}; | |
template <> | |
struct is_integral<short> | |
{ | |
static const bool value = true; | |
}; | |
template <> | |
struct is_integral<unsigned short> | |
{ | |
static const bool value = true; | |
}; | |
template <typename T> | |
struct is_floating_point | |
{ | |
static const bool value = false; | |
}; | |
template <> | |
struct is_floating_point<float> | |
{ | |
static const bool value = true; | |
}; | |
template <> | |
struct is_floating_point<double> | |
{ | |
static const bool value = true; | |
}; | |
template <> | |
struct is_floating_point<long double> | |
{ | |
static const bool value = true; | |
}; | |
template <bool B, typename T> | |
struct enable_if | |
{ | |
}; | |
template <typename T> | |
struct enable_if<true, T> | |
{ | |
typedef T type; | |
}; | |
template <typename T, T v> | |
struct integral_constant | |
{ | |
typedef T value_type; | |
typedef integral_constant<T, v> type; | |
static const T value = v; | |
operator value_type() const | |
{ | |
return value; | |
} | |
value_type operator()() const | |
{ | |
return value; | |
} | |
}; | |
template <class T> | |
struct is_arithmetic : integral_constant<bool, | |
is_integral<T>::value || | |
is_floating_point<T>::value> | |
{ | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment