// { dg-options "-std=gnu++0x" } // { dg-do "run" } // An implementation of TR1's using variadic teplates // Contributed by Douglas Gregor #include #include #include // Trivial reference_wrapper template struct reference_wrapper { reference_wrapper(T& x) : ptr(&x) { } operator T&() const { return *ptr; } T* ptr; }; template reference_wrapper ref(T& x) { return x; } template reference_wrapper cref(const T& x) { return x; } // Simple type-traits we'll need template struct add_reference { typedef T& type; }; template struct add_reference { typedef T& type; }; template struct is_same { static const bool value = false; }; template struct is_same { static const bool value = true; }; // For creating the constructor parameters of tuple<> template struct add_const_reference { typedef const T& type; }; template struct add_const_reference { typedef T& type; }; // 6.1.3 Class template tuple template class tuple; template<> class tuple<> { }; template class tuple : private tuple { typedef tuple inherited; public: tuple() { } // implicit copy-constructor is okay tuple(typename add_const_reference::type v, typename add_const_reference::type... vtail) : m_head(v), inherited(vtail...) { } template tuple(const tuple& other) : m_head(other.head()), inherited(other.tail()) { } template tuple& operator=(const tuple& other) { m_head = other.head(); tail() = other.tail(); return *this; } typename add_reference::type head() { return m_head; } typename add_reference::type head() const { return m_head; } inherited& tail() { return *this; } const inherited& tail() const { return *this; } protected: Head m_head; }; template struct make_tuple_result { typedef T type; }; template struct make_tuple_result > { typedef T& type; }; // 6.1.3.2 Tuple creation functions struct ignore_t { template ignore_t& operator=(const T&) { return *this; } } ignore; template tuple::type...> make_tuple(const Values&... values) { return tuple::type...>(values...); } template tuple tie(Values&... values) { return tuple(values...); } // 6.1.3.3 Tuple helper classes template struct tuple_size; template<> struct tuple_size > { static const std::size_t value = 0; }; template struct tuple_size > { static const std::size_t value = 1 + tuple_size >::value; }; template struct tuple_element; template struct tuple_element > { typedef typename tuple_element >::type type; }; template struct tuple_element<0, tuple > { typedef Head type; }; // 6.1.3.4 Element access template class get_impl; template class get_impl > { typedef typename tuple_element >::type Element; typedef typename add_reference::type RJ; typedef typename add_const_reference::type PJ; typedef get_impl > Next; public: static RJ get(tuple& t) { return Next::get(t.tail()); } static PJ get(const tuple& t) { return Next::get(t.tail()); } }; template class get_impl<0, tuple > { typedef typename add_reference::type RJ; typedef typename add_const_reference::type PJ; public: static RJ get(tuple& t) { return t.head(); } static PJ get(const tuple& t) { return t.head(); } }; template typename add_reference< typename tuple_element >::type >::type get(tuple& t) { return get_impl >::get(t); } template typename add_const_reference< typename tuple_element >::type >::type get(const tuple& t) { return get_impl >::get(t); } // 6.1.3.5 Relational operators inline bool operator==(const tuple<>&, const tuple<>&) { return true; } template bool operator==(const tuple& t, const tuple& u) { return t.head() == u.head() && t.tail() == u.tail(); } template bool operator!=(const tuple& t, const tuple& u) { return !(t == u); } inline bool operator<(const tuple<>&, const tuple<>&) { return false; } template bool operator<(const tuple& t, const tuple& u) { return (t.head() < u.head() || (!(t.head() < u.head()) && t.tail() < u.tail())); } template bool operator>(const tuple& t, const tuple& u) { return u < t; } template bool operator<=(const tuple& t, const tuple& u) { return !(u < t); } template bool operator>=(const tuple& t, const tuple& u) { return !(t < u); } int a0[tuple_size >::value == 0? 1 : -1]; int a1[tuple_size >::value == 3? 1 : -1]; int a2a[is_same >::type, int> ::value? 1 : -1]; int a2b[is_same >::type, float> ::value? 1 : -1]; int a2c[is_same >::type, double> ::value? 1 : -1]; int main() { tuple<> t0; tuple t1(1); tuple t2(1, 3.14159f); tuple t3a(1, 3.14159f, "Hello, world!"); tuple t3b(t3a); t3b = t3a; // t3a = t3b; DPG: triggers an error, as it should. tuple t3c = make_tuple(17, 2.718281828, std::string("Fun")); int seventeen = 17; double pi = 3.14159; tuple seventeen_pi = make_tuple(ref(seventeen), ref(pi)); tuple seventeen_pi2 = make_tuple(ref(seventeen), cref(pi)); tuple seventeen_pi_tied = tie(seventeen, pi); assert(get<0>(t3a) == 1); assert(get<1>(t3a) == 3.14159f); assert(std::strcmp(get<2>(t3a), "Hello, world!") == 0); assert(t3a == t3b); assert(!(t3a != t3b)); assert(!(t3a < t3b)); assert(!(t3a > t3b)); assert(t3a <= t3b && t3b <= t3a); assert(t3a >= t3b && t3b >= t3a); }