以下 SFINAE 友好方法似乎可以按预期工作(有关例外情况,请参见下文):
#include <type_traits>
namespace detail {
struct empty {};
template <typename T>
using base = std::conditional_t<std::is_class<T>{} && not std::is_final<T>{},
T, empty>;
struct P1 {typedef int begin, end;};
template <typename U>
struct TestMemType : base<U>, P1 {
template <typename T=TestMemType, typename=typename T::begin>
static std::true_type test_begin(int);
template <typename T=TestMemType, typename=typename T::end>
static std::true_type test_end(int);
static std::false_type test_begin(float), test_end(float);
};
template <typename T>
constexpr bool hasMember = !decltype(TestMemType<T>::test_begin(0)){}
|| !decltype(TestMemType<T>::test_end(0)){};
//! Step 1
template <typename T, std::size_t N>
constexpr auto begin(int, T(&a)[N]) {return a;}
template <typename T, std::size_t N>
constexpr auto end(int, T(&a)[N]) {return a+N;}
//! Step 2 - this overload is less specialized than the above.
template <typename T>
constexpr auto begin(int, T& a) -> decltype(a.begin()) {return a.begin();}
template <typename T>
constexpr auto end(int, T& a) -> decltype(a.end()) {return a.end();}
//! Step 3
namespace nested_detail {
void begin(), end();
template <typename T>
constexpr auto begin_(T& a) -> decltype(begin(a)) {return begin(a);}
template <typename T>
constexpr auto end_(T& a) -> decltype(end(a)) {return end(a);}
}
template <typename T, typename=std::enable_if_t<not hasMember<std::decay_t<T>>>>
constexpr auto begin(float, T& a) -> decltype(nested_detail::begin_(a))
{return nested_detail::begin_(a);}
template <typename T, typename=std::enable_if_t<not hasMember<std::decay_t<T>>>>
constexpr auto end(float, T& a) -> decltype(nested_detail::end_(a))
{return nested_detail::end_(a);}
}
template <typename T>
constexpr auto magic_begin(T& a) -> decltype(detail::begin(0, a))
{return detail::begin(0, a);}
template <typename T>
constexpr auto magic_end (T& a) -> decltype(detail::end (0, a))
{return detail:: end(0, a);}
Demo http://coliru.stacked-crooked.com/a/3c8514f56c017396。请注意,GCC 查找已损坏,因为它不考虑非类型名称typename T::begin
in TestMemType::test_end/begin
。可以找到解决方法草图here http://coliru.stacked-crooked.com/a/8abb2f9761f4ea34.
步骤 2 中的检查要求类类型是可派生的,这意味着该方法不能正确使用final
类或联合 - 如果它们具有无法访问的成员名称begin
/end
.