简体   繁体   English

没有void_t的C ++检测习语

[英]C++ detection idiom without void_t

How I can implement C++ detection idiom without using void_t ? 如何在不使用void_t情况下实现C ++ 检测习惯用法 In other words, can I implement C++17 std::is_detected etc using only C++03-features? 换句话说,我std::is_detected可以仅使用C ++ 03功能实现C ++ 17 std::is_detected等?

UPD Detection idiom requires C++11 by definition. 根据定义, UPD检测习惯用法需要C ++ 11。 In my question I just meant that I want to implement is_detected somehow without void_t . 在我的问题中,我只是想要在没有void_t情况is_detected以某种方式实现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; 我的问题在于:别名模板中未使用的参数不能保证确保SFINAE并且可以忽略,而VS 2013有这个缺陷; another tries (like on cppreference) leads to compiler crash (yep, cl is the greatest compiler in the world). 另一次尝试(比如cppreference)会导致编译器崩溃(是的, cl是世界上最好的编译器)。

UPD2 I suppose that VS 2013 can break any C++ metaprogramming techniques (and programmer's brains too). UPD2我认为VS 2013可以打破任何C ++元编程技术(以及程序员的大脑)。

Since the question was modified and C++ 11 is allowed, I post almost a copy-paste from cppreference, no credit taken. 由于问题被修改并且允许C ++ 11,我几乎从cppreference发布了一个复制粘贴,没有信用。

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. 诀窍是尽可能地滥用sizeof

Note that the declval is different from std::declval . 请注意, declvalstd::declval不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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