Files
fusion/test/sequence/tuple_traits.cpp
Lee Clagett 5906d9c316 Make C++11 fusion::vector more consistent with C++03 fusion::vector
- Construct from elements is enabled iff each argument is_convertible to
    corresponding element.
  - Construct from sequence is enabled iff the single argument is a fusion
    sequence.
  - C++11 vector and tuple also disable construct from sequence that are
    shorter than the destination. C++03 gives incorrect is_convertible
    responses in this situation and fails to compile if that constructor is
    used; C++11 can have instantation errors in
    and_<is_convertible<U, T>...> without the additional check.
  - C++11 tuple and vector support truncation conversion and assignment like
    all other sequences.
2016-08-26 12:45:11 -04:00

87 lines
3.4 KiB
C++

/*=============================================================================
Copyright (c) 2016 Lee Clagett
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/tuple/tuple.hpp>
#include <boost/config.hpp>
#include <boost/type_traits/is_constructible.hpp>
#include <boost/type_traits/is_convertible.hpp>
#define FUSION_SEQUENCE boost::fusion::tuple
#define FUSION_ALT_SEQUENCE boost::fusion::vector
#include "traits.hpp"
struct not_convertible {};
/* Some construction differences in fusion::tuple from std::tuple:
- Construction from elements cannot call an explicit constructor.
- There is no implicit construction from elements.
- Construction from std::pair is _enabled_ when tuple is not of size 2.
- Construction from tuple is _enabled_ when destination tuple is of
different size.
- Implicit construction from std::pair can call explicit constructors on
elements.
- Implicit construction from tuple can call explicit constructors on
elements.
These differences are historical. Matching the behavior of std::tuple
could break existing code, however, switching to fusion::vector would
restore the historical behavior. */
int
main()
{
using namespace boost::fusion;
test_convertible(false /* no conversion construction */ );
BOOST_TEST((is_convertible<std::pair<int, int>, tuple<int, int> >(true)));
BOOST_TEST((
is_convertible<std::pair<int, int>, tuple<convertible, int> >(true)
));
BOOST_TEST((
is_convertible<std::pair<int, int>, tuple<int, convertible> >(true)
));
BOOST_TEST((
is_convertible<
std::pair<int, int>, tuple<convertible, convertible>
>(true)
));
#if defined(FUSION_TEST_HAS_CONSTRUCTIBLE)
test_constructible();
BOOST_TEST((is_constructible< tuple<> >(true)));
BOOST_TEST((is_constructible<tuple<>, int>(false)));
BOOST_TEST((is_constructible< tuple<int> >(true)));
BOOST_TEST((is_constructible<tuple<int>, int>(true)));
BOOST_TEST((is_constructible<tuple<convertible>, int>(true)));
BOOST_TEST((is_constructible<tuple<not_convertible>, int>(false)));
BOOST_TEST((is_constructible< tuple<int>, vector<int> >(false)));
BOOST_TEST((is_constructible<tuple<int>, int, int>(false)));
BOOST_TEST((is_constructible< tuple<int, int> >(true)));
BOOST_TEST((is_constructible<tuple<int, int>, int, int>(true)));
BOOST_TEST((
is_constructible<tuple<convertible, convertible>, int, int>(true)
));
BOOST_TEST((is_constructible<tuple<int, not_convertible>, int, int>(false)));
BOOST_TEST((is_constructible<tuple<not_convertible, int>, int, int>(false)));
BOOST_TEST((
is_constructible<tuple<not_convertible, not_convertible>, int, int>(false)
));
#if defined(BOOST_FUSION_HAS_VARIADIC_VECTOR)
// C++03 fusion::tuple has constructors that can never be used
BOOST_TEST((is_constructible<tuple<int, int>, int>(false)));
#endif
BOOST_TEST((is_constructible<tuple<int, int>, int, int, int>(false)));
#endif // FUSION_TEST_HAS_CONSTRUCTIBLE
return boost::report_errors();
}