Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当X为真时断言Y也为真-暗示_C++ - Fatal编程技术网

C++ 当X为真时断言Y也为真-暗示

C++ 当X为真时断言Y也为真-暗示,c++,C++,如何断言如果X是true那么Y也是true。问题是,如果我写以下内容: assert(X && Y && "If X is true then Y should be true too."); 如果boolx=false,将失败而这也可以是有效的情况。逻辑表达式是:断言(!X | |(X&&Y)) 如果希望包含您的邮件,只需将其用括号括起来即可: assert((!X || (X && Y)) && "If X is true t

如何断言如果
X
true
那么
Y
也是
true
。问题是,如果我写以下内容:

assert(X && Y && "If X is true then Y should be true too.");

如果
boolx=false,将失败而这也可以是有效的情况。

逻辑表达式是:
断言(!X | |(X&&Y))

如果希望包含您的邮件,只需将其用括号括起来即可:

assert((!X || (X && Y)) && "If X is true then Y should be true too.");
现在,我们可以简化这个逻辑,因为我们知道如果
!X
计算为
false
(我们正在计算
|
的右侧),然后我们知道
X
必须为true,因此我们可以进一步简化它:

assert(!X || Y); // If !X is false, then X must be true, so no need to test it.

逻辑表达式是:
assert(!X | |(X&&Y))

如果希望包含您的邮件,只需将其用括号括起来即可:

assert((!X || (X && Y)) && "If X is true then Y should be true too.");
现在,我们可以简化这个逻辑,因为我们知道如果
!X
计算为
false
(我们正在计算
|
的右侧),然后我们知道
X
必须为true,因此我们可以进一步简化它:

assert(!X || Y); // If !X is false, then X must be true, so no need to test it.
然后:

或者,如果你想玩得开心

namespace implies_trick {
  struct lhs_t {
    bool v;
    constexpr lhs_t(bool b):v(b) {}
  };
  struct implies_t { constexpr implies_t() {} };
  constexpr implies_t implies = {};
  constexpr lhs_t operator->*( bool a, implies_t ) { return {a}; }
  constexpr bool operator*( lhs_t a, bool b ) { return a.v?b:true; }
}
using implies_trick::implies;

for (bool X:{true, false})
  for (bool Y:{true, false})
    std::cout << X << "->" << Y << " is " << X ->*implies* Y << "\n";
然后:

或者,如果你想玩得开心

namespace implies_trick {
  struct lhs_t {
    bool v;
    constexpr lhs_t(bool b):v(b) {}
  };
  struct implies_t { constexpr implies_t() {} };
  constexpr implies_t implies = {};
  constexpr lhs_t operator->*( bool a, implies_t ) { return {a}; }
  constexpr bool operator*( lhs_t a, bool b ) { return a.v?b:true; }
}
using implies_trick::implies;

for (bool X:{true, false})
  for (bool Y:{true, false})
    std::cout << X << "->" << Y << " is " << X ->*implies* Y << "\n";
如何断言如果X是真的,那么y也是真的

您所描述的是:
X→ Y

C++中没有蕴涵算子。但举例来说,用真值表证明

X→ Y
相当于X∨ Y
。我们可以在C++中编写,因为它既有否定又有运算符(析取):

如何断言如果X是真的,那么y也是真的

您所描述的是:
X→ Y

C++中没有蕴涵算子。但举例来说,用真值表证明

X→ Y
相当于X∨ Y。我们可以在C++中编写,因为它既有否定又有运算符(析取):


类似于雅克的漂亮回答,但语法不同:

#include <iostream>


namespace detail {

    struct when_impl
    {
        constexpr when_impl(bool condition)
        : _cond(condition)
        {}

        constexpr operator bool() const { return _cond; }
        bool _cond;
    };

    struct then_impl
    {
        constexpr then_impl(bool condition)
        : _cond(condition)
        {}

        constexpr operator bool() const { return _cond; }
        bool _cond;
    };

}

constexpr auto when(bool condition)
{
    return detail::when_impl(condition);
}

constexpr auto then(bool condition)
{
    return detail::then_impl(condition);
}

constexpr bool operator,(detail::when_impl when, detail::then_impl then)
{
    if (bool(when)) return bool(then);
    return true;
}

int main()
{
    for (bool X:{true, false})
        for (bool Y:{true, false})
            std::cout << X << "->" << Y << " is " << (when(X), then(Y)) << "\n";

    static_assert((when(true), then(true)), "Y must follow X");
    return 0;
}

(注意:static_assert在构建过程中不会触发)

类似于Yakk的漂亮答案,但语法不同:

#include <iostream>


namespace detail {

    struct when_impl
    {
        constexpr when_impl(bool condition)
        : _cond(condition)
        {}

        constexpr operator bool() const { return _cond; }
        bool _cond;
    };

    struct then_impl
    {
        constexpr then_impl(bool condition)
        : _cond(condition)
        {}

        constexpr operator bool() const { return _cond; }
        bool _cond;
    };

}

constexpr auto when(bool condition)
{
    return detail::when_impl(condition);
}

constexpr auto then(bool condition)
{
    return detail::then_impl(condition);
}

constexpr bool operator,(detail::when_impl when, detail::then_impl then)
{
    if (bool(when)) return bool(then);
    return true;
}

int main()
{
    for (bool X:{true, false})
        for (bool Y:{true, false})
            std::cout << X << "->" << Y << " is " << (when(X), then(Y)) << "\n";

    static_assert((when(true), then(true)), "Y must follow X");
    return 0;
}

(注意:静态断言在构建期间不会触发)

!X | | Y
应该做这项工作
!X | | Y
应该做大量工作来定义一个难以理解的新语法。定义一个难以理解的新语法需要大量工作。考虑简化的另一种方式是,如果
Y
为真,那么
X
是否为真并不重要。同样,如果
X
不为真,那么
Y
是否为真并不重要。考虑简化的另一种方式是,如果
Y
为真,那么
X
是否为真并不重要。同样,如果
X
不为真,那么
Y
是否为真并不重要。
1->1 is 1
1->0 is 0
0->1 is 1
0->0 is 1