Overloading short-circuit

Pages: 1234
DEATHBLOW! When someone tries to blow you up! Not because of who you are, but because of different reasons altogether!
That's simply the best (♫ better than all the rest ♫) way to win an argument. It's how I always win threads.
Last edited on
closed account (EzwRko23)
Discussions are not for making someone change their mind. :P
Discussions are for sharing views and ideas.
closed account (EzwRko23)
Exactly.
closed account (iw0XoG1T)
If this is a discussion—tell me what is being discussed?

I've noted at least three subjects:
1.) can the “&&” operator be overloaded?
2.) should C++ be abandoned for Scala?
3.) is xorebxebx opinion important enough to take seriously?

The main purpose of the lounge is to keep crap out of the other forums, and the lounge has been extremely successful--nothing said in this thread is worth noting, and it would have been sad if any of this, outside of hellos' original post, would have been posted in the other forums.

It is often said that the internets are serious business. I love that statement because it can be read on so many different levels depending on your level of maturity.
closed account (1yR4jE8b)
What has baffled me from the beginning...is why isn't something like this considered short circuit evaluation? I'm not trying to sound like a smart ass, I'd like to actually know if there is something wrong with my train of thought and why (always in the pursuit of deeper knowledge)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

struct Foo
{
	Foo(bool val) : value(val){}

	operator bool() const
	{
		std::cout << "Evaluated A Foo\n";
		return value;
	}
	
	bool value;
};

struct Bar
{
	Bar(bool val) : value(val){}
	
	operator bool() const
	{
		std::cout << "Evaluated A Bar\n";
		return value;
	}
	
	bool value;
};

template <typename T, typename E>
bool operator&&(const T& left, const E& right)
{
	bool result = false;
	
	if(!left)
		;	//no-op
	else if(right)
		result = true;
		
	return result;
}

int main()
{
	std::cout << "Foo(true) && Bar(true)) -> true" << '\n';
	std::cout << std::boolalpha << (Foo(true) && Bar(true)) << "\n\n";

	std::cout << "Foo(false) && Bar(true)) -> false" << '\n';
	std::cout << std::boolalpha << (Foo(false) && Bar(true)) << "\n\n";
	
	std::cout << "Foo(true) && Bar(false)) -> false" << '\n';
	std::cout << std::boolalpha << (Foo(true) && Bar(false)) << "\n\n";
	
	std::cout << "Foo(false) && Bar(false)) -> false" << '\n';
	std::cout << std::boolalpha << (Foo(false) && Bar(false)) << "\n\n";
	
	return 0;
}



Program Output:

$ ./a
Foo(true) && Bar(true)) -> true
Evaluated A Foo
Evaluated A Bar
true

Foo(false) && Bar(true)) -> false
Evaluated A Foo
false

Foo(true) && Bar(false)) -> false
Evaluated A Foo
Evaluated A Bar
false

Foo(false) && Bar(false)) -> false
Evaluated A Foo
false
The function on lines 29-40 is never getting called.

EDIT: My bad.
The problem is that, following short-circuit, no Bar object should be constructed if the Foo object has a false value.
Last edited on
Topic archived. No new replies allowed.
Pages: 1234