admin管理员组

文章数量:1023794

I'm updating somebody else's code.

They did the following :

bool a();
bool b();

a() || b() || std::clog << "whine about something" << std::endl;

I'm replacing the std::clog bit with a void function and the compiler doesn't like that because it can't convert void to bool.

So... I decided to go with:

if (!a() && !b()) {
 c(); // Replacement to the clog bit.
}

Would this behave the same?

Note: This code was originally likely written in c++03 or c++11, but we're using c++17 now, in the unlikely case that makes a difference.

I'm updating somebody else's code.

They did the following :

bool a();
bool b();

a() || b() || std::clog << "whine about something" << std::endl;

I'm replacing the std::clog bit with a void function and the compiler doesn't like that because it can't convert void to bool.

So... I decided to go with:

if (!a() && !b()) {
 c(); // Replacement to the clog bit.
}

Would this behave the same?

Note: This code was originally likely written in c++03 or c++11, but we're using c++17 now, in the unlikely case that makes a difference.

Share Improve this question edited Nov 18, 2024 at 17:41 Gregorio Litenstein asked Nov 18, 2024 at 17:27 Gregorio LitensteinGregorio Litenstein 6816 silver badges21 bronze badges 20
  • 2 a and b are functions? – Igor G Commented Nov 18, 2024 at 17:30
  • 2 Even if I prefer the if version, another way of rewrite a() || b() || (c(), true);. – Jarod42 Commented Nov 18, 2024 at 17:38
  • 2 They're equivalent according to de Man's Laws. – Barmar Commented Nov 18, 2024 at 17:50
  • 2 you should write unit tests. De Man's Law wont protect you from making a typo ;) – 463035818_is_not_an_ai Commented Nov 18, 2024 at 17:55
  • 5 @DrewDormann The logical value isn't important here, just the short-circuiting behavior. – Barmar Commented Nov 18, 2024 at 18:30
 |  Show 15 more comments

1 Answer 1

Reset to default 4

The equivalence of the two expressions values is covered by De Man's Law. To make sure short-cuirciting behavior is correct with regard to evaluating b() consider that if a() is true then b() will not be evaluated. This is the case for the two alternatives as you can convince yourself by examining the 4 possible cases:

#include<iostream>

template <bool s>
bool ab(bool x) {
    std::cout << (s?"a":"b") ;return x;
}
auto a = &ab<true>;
auto b = &ab<false>;
bool c() {
    std::cout << "c";
    return true;
}



void original(bool aa,bool bb) {
    a(aa) || b(bb) || c();
}
void alternative(bool aa,bool bb) {
    if (!a(aa) && !b(bb)) c();
}

void test(auto f) {
    f(true,true);
    std::cout << "\n";
    f(true,false);
    std::cout << "\n";
    f(false,true);
    std::cout << "\n";
    f(false,false);
    std::cout << "\n";
}


int main() {
    test(original);
    std::cout << "---\n";
    test(alternative);
}

Output:

a
a
ab
abc
---
a
a
ab
abc

I'm updating somebody else's code.

They did the following :

bool a();
bool b();

a() || b() || std::clog << "whine about something" << std::endl;

I'm replacing the std::clog bit with a void function and the compiler doesn't like that because it can't convert void to bool.

So... I decided to go with:

if (!a() && !b()) {
 c(); // Replacement to the clog bit.
}

Would this behave the same?

Note: This code was originally likely written in c++03 or c++11, but we're using c++17 now, in the unlikely case that makes a difference.

I'm updating somebody else's code.

They did the following :

bool a();
bool b();

a() || b() || std::clog << "whine about something" << std::endl;

I'm replacing the std::clog bit with a void function and the compiler doesn't like that because it can't convert void to bool.

So... I decided to go with:

if (!a() && !b()) {
 c(); // Replacement to the clog bit.
}

Would this behave the same?

Note: This code was originally likely written in c++03 or c++11, but we're using c++17 now, in the unlikely case that makes a difference.

Share Improve this question edited Nov 18, 2024 at 17:41 Gregorio Litenstein asked Nov 18, 2024 at 17:27 Gregorio LitensteinGregorio Litenstein 6816 silver badges21 bronze badges 20
  • 2 a and b are functions? – Igor G Commented Nov 18, 2024 at 17:30
  • 2 Even if I prefer the if version, another way of rewrite a() || b() || (c(), true);. – Jarod42 Commented Nov 18, 2024 at 17:38
  • 2 They're equivalent according to de Man's Laws. – Barmar Commented Nov 18, 2024 at 17:50
  • 2 you should write unit tests. De Man's Law wont protect you from making a typo ;) – 463035818_is_not_an_ai Commented Nov 18, 2024 at 17:55
  • 5 @DrewDormann The logical value isn't important here, just the short-circuiting behavior. – Barmar Commented Nov 18, 2024 at 18:30
 |  Show 15 more comments

1 Answer 1

Reset to default 4

The equivalence of the two expressions values is covered by De Man's Law. To make sure short-cuirciting behavior is correct with regard to evaluating b() consider that if a() is true then b() will not be evaluated. This is the case for the two alternatives as you can convince yourself by examining the 4 possible cases:

#include<iostream>

template <bool s>
bool ab(bool x) {
    std::cout << (s?"a":"b") ;return x;
}
auto a = &ab<true>;
auto b = &ab<false>;
bool c() {
    std::cout << "c";
    return true;
}



void original(bool aa,bool bb) {
    a(aa) || b(bb) || c();
}
void alternative(bool aa,bool bb) {
    if (!a(aa) && !b(bb)) c();
}

void test(auto f) {
    f(true,true);
    std::cout << "\n";
    f(true,false);
    std::cout << "\n";
    f(false,true);
    std::cout << "\n";
    f(false,false);
    std::cout << "\n";
}


int main() {
    test(original);
    std::cout << "---\n";
    test(alternative);
}

Output:

a
a
ab
abc
---
a
a
ab
abc

本文标签: c17c Is (ab)c equivalent to if (a ampamp b)c Stack Overflow