clang produces a different result than gcc

Hi guys,
This code:
#include <iostream>
using namespace std;

int someFunction(int var1, int var2){
int total = var1 + var2;
cout << "The total of someFunction is: " << total << endl;

return total;
}

int main(){
someFunction(7, 8);
cout << "The return value of someFunction is:" << someFunction(7, 8) << endl;


return 0;
}

produces this result when compiled with g++:
The total of someFunction is: 15
The return value of someFunction is:15

and this is the result when compiled with clang++:
The total of someFunction is: 15
The return value of someFunction is:The total of someFunction is: 15
15

Is there an error with Clang or Gcc and why does using different compilers produce a different result?
Also, do you prefer clang or gcc and which one should I use?

Thanks guys :-)
Last edited on
cout << "The return value of someFunction is:" << someFunction(7, 8) << endl
One compiler is evaluating this expression in this order:
1
2
3
4
temp2 = someFunction(7, 8);
temp = cout << "The return value of someFunction is:";
temp = temp << temp2;
temp = temp << endl;
The other is evaluating in this order:
1
2
3
4
temp = cout << "The return value of someFunction is:";
temp2 = someFunction(7, 8);
temp = temp << temp2;
temp = temp << endl;

The rules of the language permit both orders. Neither compiler is in error.
> why does using different compilers produce a different result?

The function calls are indeterminately sequenced. Note that the << is a function call here.
http://en.cppreference.com/w/cpp/language/eval_order


> do you prefer clang or gcc

clang++. Primarily because of libc++, and also because it is non-viral software.


> and which one should I use?

Use both, if you can. Your code has a better chance of being standard-conforming if it is compiled with both. Force conformance, and enable warnings with: -std=c++14 -pedantic-errors -Wall -Wextra
helios:
Ah, that clears things up. Thanks :-)
JLBorges:
Thanks for your help.
I have heard that clang is a much better compiler than gcc, do you think that clang will replace gcc in the future?
> I have heard that clang is a much better compiler than gcc,

With their current versions, there is very little to choose between the two. The architecture and internals of clang++ and libc++ are a lot cleaner and more malleable than that of g++/libstdc++, so one would expect that the LLVM tool-chain would evolve more gracefully.


> do you think that clang will replace gcc in the future?

No, each has its own strengths. The use of clang++ has been increasing, and would continue to increase; but it is not going to completely replace g++ in the foreseeable future.

The GNU compiler and tool-chain are available on many more platforms, and a large amount of legacy code still depends on the GNU tool-chain.

The LLVM software is more transparent, and free in the true sense of the word; so it is more likely to get enriched by contributions from neutral developers. For instance, Microsoft has ported the clang++ front-end and integrated it with the Visual Studio development environment.
Topic archived. No new replies allowed.