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