add const_reference_cast

This commit is contained in:
bolero-MURAKAMI 2016-04-19 09:05:41 +09:00
parent 8efb1a3748
commit bd2194e331
11 changed files with 215 additions and 26 deletions

View file

@ -24,13 +24,13 @@ namespace sprout {
// [default]
// Container is T[N] -> cont[sprout::range_index_check(cont, i)]
// otherwise, Container is not const
// && sprout::is_const_cast_convertible<const_reference, reference>
// && sprout::is_const_reference_cast_convertible<const_reference, reference>
// && (callable sprout::as_const(cont).at(i)
// || callable sprout::as_const(cont)[i]
// || callable sprout::as_const(cont).begin()
// || ADL(without sprout) callable begin(sprout::as_const(cont))
// )
// -> const_cast<reference>(sprout::at(sprout::as_const(cont), i))
// -> sprout::const_reference_cast<reference>(sprout::at(sprout::as_const(cont), i))
// otherwise, callable cont.at(i) -> cont.at(i)
// otherwise, callable cont[i] -> cont[sprout::range_index_check(cont, i)]
// otherwise -> *sprout::next(sprout::begin(cont), sprout::range_index_check(cont, i))

View file

@ -24,12 +24,12 @@ namespace sprout {
// [default]
// Container is T[N] -> cont[N - 1]
// otherwise, Container is not const
// && sprout::is_const_cast_convertible<const_reference, reference>
// && sprout::is_const_reference_cast_convertible<const_reference, reference>
// && (callable sprout::as_const(cont).back()
// || callable sprout::as_const(cont).begin()
// || ADL(without sprout) callable begin(sprout::as_const(cont))
// )
// -> const_cast<reference>(sprout::back(sprout::as_const(cont)))
// -> sprout::const_reference_cast<reference>(sprout::back(sprout::as_const(cont)))
// otherwise, callable cont.back() -> cont.back()
// otherwise -> *sprout::next(sprout::begin(cont), sprout::size(cont) - 1)
//

View file

@ -13,7 +13,6 @@
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/is_const_cast_convertible.hpp>
#include <sprout/container/traits_fwd.hpp>
#include <sprout/container/container_traits.hpp>
#include <sprout/container/range_functions_fwd.hpp>
@ -54,7 +53,7 @@ namespace sprout {
template<typename T>
struct is_substitutable_const_at
: public sprout::bool_constant<
sprout::is_const_cast_convertible<
sprout::is_const_reference_cast_convertible<
typename sprout::container_traits<T const>::reference,
typename sprout::container_traits<T>::reference
>::value
@ -74,7 +73,7 @@ namespace sprout {
>::type
range_at_impl(Container& cont, typename sprout::container_traits<Container>::size_type i) {
typedef typename sprout::container_traits<Container>::reference type;
return const_cast<type>(sprout::at(sprout::as_const(cont), i));
return sprout::const_reference_cast<type>(sprout::at(sprout::as_const(cont), i));
}
template<typename Container>
inline SPROUT_CONSTEXPR typename std::enable_if<

View file

@ -13,7 +13,6 @@
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/is_const_cast_convertible.hpp>
#include <sprout/container/traits_fwd.hpp>
#include <sprout/container/container_traits.hpp>
#include <sprout/container/range_functions_fwd.hpp>
@ -53,7 +52,7 @@ namespace sprout {
template<typename T>
struct is_substitutable_const_back
: public sprout::bool_constant<
sprout::is_const_cast_convertible<
sprout::is_const_reference_cast_convertible<
typename sprout::container_traits<T const>::reference,
typename sprout::container_traits<T>::reference
>::value
@ -72,7 +71,7 @@ namespace sprout {
>::type
range_back_impl(Container& cont) {
typedef typename sprout::container_traits<Container>::reference type;
return const_cast<type>(sprout::back(sprout::as_const(cont)));
return sprout::const_reference_cast<type>(sprout::back(sprout::as_const(cont)));
}
template<typename Container>
inline SPROUT_CONSTEXPR typename std::enable_if<

View file

@ -13,11 +13,11 @@
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/is_const_cast_convertible.hpp>
#include <sprout/container/traits_fwd.hpp>
#include <sprout/container/container_traits.hpp>
#include <sprout/container/range_functions_fwd.hpp>
#include <sprout/container/detail/range_begin.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/utility/as_const.hpp>
#include <sprout/adl/not_found.hpp>
@ -52,7 +52,7 @@ namespace sprout {
template<typename T>
struct is_substitutable_const_front
: public sprout::bool_constant<
sprout::is_const_cast_convertible<
sprout::is_const_reference_cast_convertible<
typename sprout::container_traits<T const>::reference,
typename sprout::container_traits<T>::reference
>::value
@ -71,7 +71,7 @@ namespace sprout {
>::type
range_front_impl(Container& cont) {
typedef typename sprout::container_traits<Container>::reference type;
return const_cast<type>(sprout::front(sprout::as_const(cont)));
return sprout::const_reference_cast<type>(sprout::front(sprout::as_const(cont)));
}
template<typename Container>
inline SPROUT_CONSTEXPR typename std::enable_if<

View file

@ -13,7 +13,6 @@
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/is_const_cast_convertible.hpp>
#include <sprout/container/traits_fwd.hpp>
#include <sprout/container/container_traits.hpp>
#include <sprout/container/range_functions_fwd.hpp>
@ -53,7 +52,7 @@ namespace sprout {
template<typename T>
struct is_substitutable_const_subscript_at
: public sprout::bool_constant<
sprout::is_const_cast_convertible<
sprout::is_const_reference_cast_convertible<
typename sprout::container_traits<T const>::reference,
typename sprout::container_traits<T>::reference
>::value
@ -72,7 +71,7 @@ namespace sprout {
>::type
range_subscript_at_impl(Container& cont, typename sprout::container_traits<Container>::size_type i) {
typedef typename sprout::container_traits<Container>::reference type;
return const_cast<type>(sprout::subscript_at(sprout::as_const(cont), i));
return sprout::const_reference_cast<type>(sprout::subscript_at(sprout::as_const(cont), i));
}
template<typename Container>
inline SPROUT_CONSTEXPR typename std::enable_if<

View file

@ -24,12 +24,12 @@ namespace sprout {
// [default]
// Container is T[N] -> cont[0]
// otherwise, Container is not const
// && sprout::is_const_cast_convertible<const_reference, reference>
// && sprout::is_const_reference_cast_convertible<const_reference, reference>
// && (callable sprout::as_const(cont).front()
// || callable sprout::as_const(cont).begin()
// || ADL(without sprout) callable begin(sprout::as_const(cont))
// )
// -> const_cast<reference>(sprout::front(sprout::as_const(cont)))
// -> sprout::const_reference_cast<reference>(sprout::front(sprout::as_const(cont)))
// otherwise, callable cont.front() -> cont.front()
// otherwise -> *sprout::begin(cont)
//

View file

@ -24,12 +24,12 @@ namespace sprout {
// [default]
// Container is T[N] -> cont[i]
// otherwise, Container is not const
// && sprout::is_const_cast_convertible<const_reference, reference>
// && sprout::is_const_reference_cast_convertible<const_reference, reference>
// && (callable sprout::as_const(cont)[i]
// || callable sprout::as_const(cont).begin()
// || ADL(without sprout) callable begin(sprout::as_const(cont))
// )
// -> const_cast<reference>(sprout::subscript_at(sprout::as_const(cont), i))
// -> sprout::const_reference_cast<reference>(sprout::subscript_at(sprout::as_const(cont), i))
// otherwise, callable cont[i] -> cont[i]
// otherwise -> *sprout::next(sprout::begin(cont), i)
//

View file

@ -12,7 +12,6 @@
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/is_same.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/enabler_if.hpp>
#include <sprout/adl/not_found.hpp>
@ -86,7 +85,7 @@ namespace sprout {
{};
template<typename From, typename To>
struct is_const_iterator_cast_convertible<From*, To*>
: public std::is_same<typename std::remove_const<From>::type, typename std::remove_const<To>::type>
: public sprout::is_same<typename std::remove_const<From>::type, typename std::remove_const<To>::type>
{};
} // namespace sprout
@ -98,8 +97,7 @@ namespace sprout_adl {
namespace sprout {
namespace iterator_detail {
template<
typename T,
typename U,
typename T, typename U,
typename sprout::enabler_if<sprout::is_const_iterator_cast_convertible<U*, T>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
@ -117,7 +115,7 @@ namespace sprout {
namespace sprout_iterator_detail {
template<typename To, typename From>
inline SPROUT_CONSTEXPR To
const_iterator_cast(From const& it) {
call_const_iterator_conversion(From const& it) {
using sprout::iterator_detail::const_iterator_conversion;
using sprout_adl::const_iterator_conversion;
return const_iterator_conversion<To>(it);
@ -131,7 +129,7 @@ namespace sprout {
template<typename To, typename From>
inline SPROUT_CONSTEXPR To
const_iterator_cast(From const& it) {
return sprout_iterator_detail::const_iterator_cast<To>(it);
return sprout_iterator_detail::call_const_iterator_conversion<To>(it);
}
} // namespace sprout

View file

@ -0,0 +1,192 @@
/*=============================================================================
Copyright (c) 2011-2016 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
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)
=============================================================================*/
#ifndef SPROUT_ITERATOR_CONST_REFERENCE_CAST_HPP
#define SPROUT_ITERATOR_CONST_REFERENCE_CAST_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/is_const_cast_convertible.hpp>
#include <sprout/type_traits/remove_cvref.hpp>
#include <sprout/type_traits/enabler_if.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout {
//
// is_const_reference_cast_convertible
//
template<typename From, typename To>
struct is_const_reference_cast_convertible
: public sprout::bool_constant<
sprout::is_const_cast_convertible<From, To>::value
|| std::is_same<typename sprout::remove_cvref<From>::type, typename std::remove_cv<To>::type>::value
>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From, To const>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From, To volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From, To const volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const, To>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const, To const>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const, To volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const, To const volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From volatile, To>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From volatile, To const>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From volatile, To volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From volatile, To const volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const volatile, To>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const volatile, To const>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const volatile, To volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
template<typename From, typename To>
struct is_const_reference_cast_convertible<From const volatile, To const volatile>
: public sprout::is_const_reference_cast_convertible<From, To>
{};
} // namespace sprout
namespace sprout_adl {
template<typename To>
sprout::not_found_via_adl const_reference_conversion(...);
} // namespace sprout_adl
namespace sprout {
namespace iterator_detail {
template<
typename T, typename U,
typename sprout::enabler_if<
sprout::is_const_reference_cast_convertible<U&, T>::value
&& std::is_reference<T>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
const_reference_conversion(U& t) {
return const_cast<T>(t);
}
template<
typename T, typename U,
typename sprout::enabler_if<
sprout::is_const_reference_cast_convertible<U const&, T>::value
&& std::is_reference<T>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
const_reference_conversion(U const& t) {
return const_cast<T>(t);
}
template<
typename T, typename U,
typename sprout::enabler_if<
sprout::is_const_reference_cast_convertible<U&, T>::value
&& std::is_pointer<T>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
const_reference_conversion(U& t) {
return const_cast<T>(t);
}
template<
typename T, typename U,
typename sprout::enabler_if<
sprout::is_const_reference_cast_convertible<U const&, T>::value
&& std::is_pointer<T>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
const_reference_conversion(U const& t) {
return const_cast<T>(t);
}
template<
typename T, typename U,
typename sprout::enabler_if<
sprout::is_const_reference_cast_convertible<U&, T>::value
&& !std::is_reference<T>::value && !std::is_pointer<T>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
const_reference_conversion(U& t) {
return static_cast<T>(t);
}
template<
typename T, typename U,
typename sprout::enabler_if<
sprout::is_const_reference_cast_convertible<U const&, T>::value
&& !std::is_reference<T>::value && !std::is_pointer<T>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
const_reference_conversion(U const& t) {
return static_cast<T>(t);
}
} // namespace iterator_detail
} // namespace sprout
namespace sprout_iterator_detail {
template<typename To, typename From>
inline SPROUT_CONSTEXPR To
call_const_reference_conversion(From&& t) {
using sprout::iterator_detail::const_reference_conversion;
using sprout_adl::const_reference_conversion;
return const_reference_conversion<To>(SPROUT_FORWARD(From, t));
}
} // namespace sprout_iterator_detail
namespace sprout {
//
// const_reference_cast
//
template<typename To, typename From>
inline SPROUT_CONSTEXPR To
const_reference_cast(From&& t) {
return sprout_iterator_detail::call_const_reference_conversion<To>(SPROUT_FORWARD(From, t));
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_CONST_REFERENCE_CAST_HPP

View file

@ -13,5 +13,7 @@
#include <sprout/iterator/prev.hpp>
#include <sprout/iterator/distance.hpp>
#include <sprout/iterator/advance.hpp>
#include <sprout/iterator/const_iterator_cast.hpp>
#include <sprout/iterator/const_reference_cast.hpp>
#endif // #ifndef SPROUT_ITERATOR_OPERATION_HPP