Reputation: 407
How I can implement C++ detection idiom without using void_t
? In other words, can I implement C++17 std::is_detected
etc using only C++03-features?
UPD Detection idiom requires C++11 by definition. In my question I just meant that I want to implement is_detected
somehow without void_t
. My problem is in it: unused parameters in alias templates were not guaranteed to ensure SFINAE and could be ignored, ans VS 2013 has this defect; another tries (like on cppreference) leads to compiler crash (yep, cl
is the greatest compiler in the world).
UPD2 I suppose that VS 2013 can break any C++ metaprogramming techniques (and programmer's brains too).
Upvotes: 3
Views: 1351
Reputation: 21160
Back in the good old days, this is how we do it
template<typename T>
T declval();
template<typename T>
struct can_foo
{
typedef char yes;
struct no {char c[2];};
template<typename U>
static yes check(int (*)[sizeof(declval<U>().foo(), 1)]);
template<typename>
static no check(...);
enum {value = sizeof(check<T>(0)) == sizeof(yes)};
};
struct fooer
{
void foo() {}
};
struct barer
{
void bar() {}
};
#include<cassert>
int main()
{
assert(can_foo<fooer>::value);
assert(!can_foo<barer>::value);
}
The trick is to abuse sizeof
as much as possible.
Note that the declval
is different from std::declval
.
Upvotes: 4
Reputation: 4173
Since the question was modified and C++ 11 is allowed, I post almost a copy-paste from cppreference, no credit taken.
namespace detail {
struct nonesuch {
nonesuch() = delete;
~nonesuch() = delete;
nonesuch(nonesuch const&) = delete;
void operator=(nonesuch const&) = delete;
};
template <class Default, class AlwaysVoid, template <class...> class Op, class... Args>
struct detector {
using value_t = std::false_type;
using type = Default;
};
template <typename... Ts>
struct my_make_void {
typedef void type;
};
template <typename... Ts>
using my_void_t = typename my_make_void<Ts...>::type;
template <class Default, template <class...> class Op, class... Args>
struct detector<Default, my_void_t<Op<Args...>>, Op, Args...> {
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
template <template <class...> class Op, class... Args>
using is_detected =
typename detail::detector<detail::nonesuch, void, Op, Args...>::value_t;
template <class T>
using copy_assign_t = decltype(std::declval<T&>() = std::declval<const T&>());
struct Meow {};
struct Purr {
void operator=(const Purr&) = delete;
};
int main() {
cerr << is_detected<copy_assign_t, Meow>::value << endl;
return 0;
}
Upvotes: 3