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?
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
> 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.