如何一个bool和它的不值是假的? [英] How can both a bool and its not value be false?

查看:155
本文介绍了如何一个bool和它的不值是假的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解决这个(un)问题< a>,我注意到一个非常奇怪的行为,简而言之,使 bool 是false和它的值)也为false。我想知道这是怎么可能的。导致此的代码如下:

While attempting to solve this (un)problem, I noticed a very strange behaviour which in short made possible that a bool is false and its ! (not value) is also false. I would like to know how this is possible. The code that causes this follows:

template<typename T, typename TID = unsigned int>
struct AId {
  typedef AId<T, TID> type;
  typedef T handled_type;
  typedef TID value_type;

private:
  value_type id;

  template<typename _T> struct IsIncrementable
  {
    template<typename _U> using rm_ref = typename std::remove_reference<_U>::type;
    typedef char (&yes)[1];
    typedef char (&no)[2];
    template<class _U>
    static yes test(_U *data, typename std::enable_if<
                      std::is_same<_U, rm_ref<decltype(++(*data))>>::value
                    >::type * = 0);
    static no test(...);
    static const bool value = sizeof(yes) == sizeof(test((rm_ref<_T> *)0));
  };

public:
  explicit AId(const value_type &id) : id(id) {}

...

  //IsIncrementable<value_type>::value is false:
  //error: no type named 'type' in 'struct std::enable_if<false, int>'
  template<typename std::enable_if<IsIncrementable<value_type>::value, int>::type = 0>
  type operator++(int /*postfix*/) { type old(id); ++id; return old; }

  //!IsIncrementable<value_type>::value is also false:
  //error: no type named 'type' in 'struct std::enable_if<false, int>'
  template<typename std::enable_if<!IsIncrementable<value_type>::value, int>::type = 0>
  type operator++(int /*postfix*/) { type old(id); ++id; return old; }
};

如何可能 IsIncrementable< value_type> :: value 为false且!IsIncrementable< value_type> :: value 也为false?

How is it possible that IsIncrementable<value_type>::value is false and !IsIncrementable<value_type>::value is also false?

推荐答案

这可能对其他成员不公平,因为问题不在于模板的代码,而是实例化。

This is probably unfair to other members, as the problem doesn't lie within the code of the template, but rather with the instantiations.

@ Barry的answear解释了为什么SFINAE不会在这里工作,给出硬错误。但是如果我用以下替换函数:

@Barry's answear explains why SFINAE wouldn't work here, giving hard errors instead. But if I replace the functions with:

  static_assert(IsIncrementable<value_type>::value, "It cannot be incrementable");
  static_assert(!IsIncrementable<value_type>::value, "It has to be incrementable");

我仍然在两个断言中都有错误。问题是,我用两个不同的类实例化模板。对于一个 IsIncrementable< value_type> :: value true ,对于其他 IsIncrementable< value_type> :: value false 。因为错误会出现在 false ,这给我的印象是它总是false,即使它的值。

I still get errors in both assertions. The problem is that I was instantiating the template with two different classes. For one case IsIncrementable<value_type>::value was true and for the other IsIncrementable<value_type>::value was false. Since the error would show up on false, this gave me the impression that it was always false, even its ! value.

这篇关于如何一个bool和它的不值是假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆