Update
I posted a working rough draft of rebind
as an answer to the question. Though I didn't have much luck finding a generic way to keep static_assert
s from breaking metafunctions.
Basically I want to check if a templated type T<U, Args...>
can be constructed from some other type T<V, Args...>
. Where T
and Args...
is the same in both types. The problem is, T<>
might have a static_assert
in it that totally breaks my metafunction.
Below is a rough summary of what I'm trying to do.
template<typename T>
struct fake_alloc {
using value_type = T;
};
template<typename T, typename Alloc = fake_alloc<T>>
struct fake_cont {
using value_type = T;
// comment the line below out, and it compiles, how can I get it to compile without commenting this out???
static_assert(std::is_same<value_type, typename Alloc::value_type>::value, "must be the same type");
};
template<typename T, typename U, typename = void>
struct sample_rebind {
using type = T;
};
template<template<typename...> class Container, typename T, typename U, typename... OtherArgs>
struct sample_rebind<
Container<T, OtherArgs...>,
U,
std::enable_if_t<
std::is_constructible<
Container<T, OtherArgs...>,
Container<U, OtherArgs...>
>::value
>
>
{
using type = Container<U, OtherArgs...>;
};
static_assert(
std::is_same<
fake_cont<int, fake_alloc<int>>,
typename sample_rebind<fake_cont<int>, double>::type
>::value,
"This should pass!"
);
As you can see the desired behavior is that the final static_assert
should pass, but unfortunately, it doesn't even get to that point as the static_assert
in fake_cont
is triggered when std::is_constructible<>
attempts to call fake_cont
's constructor.
In the real code fake_cont
is libc++'s std::vector
, so I can't modify it's guts, or std::is_constructible
's guts.
Any advice for working around this specific issue is appreciated, and any advice in general for SFINAE'ing around static_assert
's is especially appreciated.
Edit: the first part of the is_same should have been fake_cont<int, fake_alloc<int>>
Edit 2: If you comment out the static_assert
in fake_cont
, it compiles (clang 3.5). And that's what I want. So I just need some way to avoid the static_assert
in fake_cont
.
namespace details {
template<class T,class=void>
struct extra_test_t: std::true_type {};
}
We then fold an extra test in:
template<class...>struct types{using type=types;};
template<template<typename...> class Container, typename T, typename U, typename... OtherArgs>
struct sample_rebind<
Container<T, OtherArgs...>,
U,
std::enable_if_t<
details::extra_test_t< types< Container<T, OtherArgs...>, U > >::value
&& std::is_constructible<
Container<T, OtherArgs...>,
Container<U, OtherArgs...>
>::value
>
> {
using type = Container<U, OtherArgs...>;
};
and we write the extra test:
namespace details {
template<class T, class Alloc, class U>
struct extra_test_t<
types<std::vector<T,Alloc>, U>,
typename std::enable_if<
std::is_same<value_type, typename Alloc::value_type>::value
>::type
> : std::true_type {};
template<class T, class Alloc, class U>
struct extra_test_t<
types<std::vector<T,Alloc>, U>,
typename std::enable_if<
!std::is_same<value_type, typename Alloc::value_type>::value
>::type
> : std::false_type {};
}
basically, this lets us inject "patches" on our test to match the static_assert
.
If we had is_std_container<T>
and get_allocator<T>
, we could write:
namespace details {
template<template<class...>class Z,class T, class...Other, class U>
struct extra_test_t<
types<Z<T,Other...>>, U>,
typename std::enable_if<
is_std_container<Z<T,Other...>>>::value
&& std::is_same<
value_type,
typename get_allocator<Z<T,Other...>>::value_type
>::value
>::type
> : std::true_type {};
template<class T, class Alloc, class U>
struct extra_test_t<
types<std::vector<T,Alloc>, U>,
typename std::enable_if<
is_std_container<Z<T,Other...>>>::value
&& !std::is_same<
value_type,
typename get_allocator<Z<T,Other...>>::value_type
>::value
>::type
> : std::false_type {};
}
or we could just state that anything with an allocator_type
probably cannot be rebound.
A more container-aware approach to this problem would be to extract the allocator type (::allocator_type
), and replace all instances of the allocator type in the container argument list with a rebind of T
to U
somehow. This is still tricky, as std::map<int, int>
has an allocator of type std::allocator< std::pair<const int, int> >
, and distinguishing between the key int
and the value int
isn't possible in a generic way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With