diff --git a/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex b/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex index b366e999da4..8689874bea1 100644 --- a/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex +++ b/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex @@ -33,7 +33,8 @@ is taken from Boost. Some free functions part of the standard interface of \ccc{tuple} are also brought in namespace \ccc{CGAL::cpp0x} with using declarations, these are \ccc{make_tuple}, -\ccc{get}, \ccc{tie}. +\ccc{get}, \ccc{tie}. Like in C++0x, the \ccc{get} function template is +specialized so that it can take \ccc{std::pair} as argument. Two standard helper classes are also provided for convenience (\ccc{tuple_size} and \ccc{tuple_element}). \end{ccRefClass} diff --git a/STL_Extension/include/CGAL/tuple.h b/STL_Extension/include/CGAL/tuple.h index 65699b3c76e..698df419e55 100644 --- a/STL_Extension/include/CGAL/tuple.h +++ b/STL_Extension/include/CGAL/tuple.h @@ -27,12 +27,15 @@ #ifndef CGAL_CFG_NO_CPP0X_TUPLE # include -#elif !defined CGAL_CFG_NO_TR1_TUPLE -# include -#else -# include -# include #endif +#ifndef CGAL_CFG_NO_TR1_TUPLE +# include +# include +#endif + +#include +#include +#include namespace CGAL { @@ -68,6 +71,47 @@ struct tuple_element: public boost::tuples::element{}; #endif + +#if defined(CGAL_CFG_NO_CPP0X_TUPLE) && defined(CGAL_CFG_NO_TR1_TUPLE) +// If not TR1 or C++11 tuple, we need to add get(std::pair). + +//////////////////////////////////////////////////////////// +// // +// Allow CGAL::cpp0x::get(std::pair), if N==0 or N==1. // +// // +// That is already in TR1 and C++11, but not in Boost. // +// // +//////////////////////////////////////////////////////////// +template +struct pair_get; + +template +struct pair_get<0, T1, T2> { + static T1& get(std::pair& pair) { return pair.first; } + static const T1& get(const std::pair& pair) { return pair.first; } +}; // end specialization struct pair_get<0, T2, T2> + +template +struct pair_get<1, T1, T2> { + static T2& get(std::pair& pair) { return pair.second; } + static const T2& get(const std::pair& pair) { return pair.second; } +}; // end specialization struct pair_get<0, T2, T2> + +template +inline typename boost::tuples::element >::type& +get(std::pair& pair) { + return pair_get::get(pair); +} + +template +inline const typename boost::tuples::element >::type& +get(const std::pair& pair) { + return pair_get::get(pair); +} + +#endif // end if not C++11 tuple + + } // cpp0x #ifndef CGAL_CFG_NO_CPP0X_VARIADIC_TEMPLATES diff --git a/STL_Extension/test/STL_Extension/test_stl_extension.cpp b/STL_Extension/test/STL_Extension/test_stl_extension.cpp index 142fbd51689..75e8bbbb05e 100644 --- a/STL_Extension/test/STL_Extension/test_stl_extension.cpp +++ b/STL_Extension/test/STL_Extension/test_stl_extension.cpp @@ -8110,10 +8110,7 @@ void test_tuple(){ T1 t1=CGAL::cpp0x::make_tuple(1,2); T1 t1_2=CGAL::cpp0x::make_tuple(1,2); - int i1=-1,i2=-1; - CGAL::cpp0x::tie(i1,i2)=t1; - CGAL_assertion( CGAL::cpp0x::get<0>(t1)==i1 ); - CGAL_assertion( CGAL::cpp0x::get<1>(t1)==i2 ); + CGAL_assertion(t1==t1_2); // test the equality operator // T2 t2 = T2(); @@ -8122,6 +8119,22 @@ void test_tuple(){ // Do not test equality between default initialized tuples, because // GNU/g++ version 4.1.2 does not default-initialize correctly // std::tr1::tuple. + + // Test CGAL::cpp0x::tie + int i1=-1,i2=-1; + CGAL::cpp0x::tie(i1,i2)=t1; + CGAL_assertion( CGAL::cpp0x::get<0>(t1)==i1 ); + CGAL_assertion( CGAL::cpp0x::get<1>(t1)==i2 ); + + // Test CGAL::cpp0x::get for a pair + double d = 1; + std::pair pair(-3, &d); + const std::pair const_pair(2, &d); + + assert(CGAL::cpp0x::get<0>(pair) == -3); + assert(CGAL::cpp0x::get<1>(pair) == &d); + assert(CGAL::cpp0x::get<0>(const_pair) == 2); + assert(CGAL::cpp0x::get<1>(const_pair) == &d); } void test_prev_next()