« first day (1569 days earlier)      last day (1508 days later) » 

07:03
Hi folks, how to prevent compiler from doing optimization?

class Animal {

public:
	Animal()
	{
		cout << this << ": default" << endl;
	}
	Animal(const Animal& old)
	{
		cout << this << ": copy" << endl;
	}
	~Animal()
	{
		cout << this << ": ~" << endl;
	}
};

Animal Gate(Animal animal)
{
	cout << "--- Gate ---" << endl;
	return animal;
}

int main()
{
	Animal x;


	// Gate(x); // 1st case, 2 copies occur

	Animal y = Gate(x); // 2nd case, 3 copies should occur
}
I want compiler no to optimize the 2nd case. Please ping me if you answer this question. Thank you!
 
5 hours later…
11:48
@MoneyOrientedProgrammer you can't? By the standard it shouldn't copy much at all
12:13
@MoneyOrientedProgrammer switching back to C++14 (or older) and setting: -fno-elide-constructors in compiler options should do the trick
I don't think you can do anything about it in C++17 though.
eh.. running away from the standard is really not recommended
I would agree, but that's his only shot.
 
2 hours later…
14:19
@kenkar Thank you. I am trying to figure out how to do this in visual studio community.
@Mgetz Thank you !

« first day (1569 days earlier)      last day (1508 days later) »