Skip to content

Instantly share code, notes, and snippets.

@johnmcfarlane
Created December 12, 2016 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmcfarlane/d17894620ede8d140d4de2f2be1f75f0 to your computer and use it in GitHub Desktop.
Save johnmcfarlane/d17894620ede8d140d4de2f2be1f75f0 to your computer and use it in GitHub Desktop.
possible implementation of proposed standard library additions, std::value_type and std::value_type_t
#include <array>
#include <atomic>
#include <complex>
#include <cstddef>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
// https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/eINuhhjZgsw
namespace experimental {
////////////////////
// value_type<>
// declaration
template<class T>
struct value_type;
// specialization for C array
template<class T, int N>
struct value_type<T[N]> {
using type = T;
};
template<typename T>
struct value_type {
using type = typename decay<T>::type::value_type;
};
////////////////////
// value_type_t<>
template<class T> using value_type_t = typename value_type<T>::type;
}
////////////////////
// test helpers
template<class Actual, class Expected>
struct assert_same {
static_assert(is_same<Actual, Expected>::value, "test failed");
};
template<class T, class ValueType>
struct test
: pair<
// test experimental::value_type
assert_same<typename experimental::value_type<T>::type, ValueType>,
// test experimental::value_type_t
assert_same<experimental::value_type_t<T>, ValueType>> {
};
////////////////////
// tests
template
struct test<char[7], char>;
template
struct test<vector<unique_ptr<const string>>&&, unique_ptr<const string>>;
template
struct test<list<reference_wrapper<volatile signed char**>>, reference_wrapper<volatile signed char**>>;
template
struct test<const vector<bool>&, bool>;
template
struct test<const bool[7], const bool>;
template
struct test<complex<float>, float>;
template
struct test<allocator<atomic<long long>>, atomic<long long>>;
template
struct test<array<void*&(double, ...), 0>, void*&(double, ...)>;
template
struct test<
unordered_map<string, size_t (array<reference_wrapper<short>, 9>::*)>,
pair<const string, unsigned long array<reference_wrapper<short>, 9>::*>>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment