简体   繁体   中英

C++ detection idiom without void_t

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).

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;
}

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);
}

Live example

The trick is to abuse sizeof as much as possible.

Note that the declval is different from std::declval .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM