I tried Geany and CodeBlocks and found it took too long to debug with those, and went with VS C++, and was much more productive right off the bat.
However, I'm testing out CLion, and using it to compile some of my VC++ code, which compiled ok in VC++, and I'm running into errors when compiling with GCC as the compiler for CLion.
Had something to do with ofstream needed to be a reference, not by value. Compiled ok in VC++, blew up in GCC, with really not a very helpful error message.
So, if I wanted to make sure I learn the best, should I be letting GCC dictate how I code?
Not trying to start an opinion war, just trying to gauge what route to take. The more permissive (I assume) VC++ is hard enough already without making things harder, but I'm looking at the long term.
> The more permissive (I assume) VC++ is hard enough already without making things harder
Therein lies the trap.
Permissiveness is the opposite of correctness.
You need to learn C++ (the language), not what any particular implementation (VC++, GCC, Clang) will let you get away with. In your programming life, you'll use many compilers, so you can't afford to learn a bunch of "VC++ does this, GCC does that" rules.
JLBorges suggestion of running multiple compilers is a good one. It might not seem like it at the moment, but getting something to compile and work on one compiler is quite easy.
But if you can compile the same code with two compilers, and get the same run-time results, you can be fairly confident that you're doing it right.
> Compiled ok in VC++, blew up in GCC, with really not a very helpful error message.
Barring obscure compiler bugs, you would believe GCC in this instance. Always go with the most pessimistic assessment of your code.
Had you not tried GCC, you would have carried on with your mistaken "ofstream by value" idea. It might work for now, but at some point it would stop working. But by then, the "bug" would also in your mind (as well as in your code), and mistaken ideas can be hard to shift.
You would get this error if you are using a somewhat old version of GCC and try to pass file streams as rvalues. They were somewhat late in implementing move semantics for file based streams.
In function 'int main()':
11:17: error: use of deleted function 'Foo::Foo(const Foo&)'
3:5: note: declared here
6:6: error: initializing argument 1 of 'void bar(Foo)'
The difference between Foo and basic_ofstream is that the latter is a template and is in namespace std.
Furthermore, your code did not have verbatim basic_ofstream, did it?
You had ofstream that is an alias for basic_ofstream<char, std::char_traits<char>>
Standard Library uses templates etc and that tends to make the error messages verbose. Some compilers might use more humane language than others, but the one that says nothing is not the one you want to use.
IDE is not compiler. IDE uses a compiler. IDE might help on formatting the error messages.
No compiler has 100% support for the entire C++ Standard.
Most have extensions that allow nonstandard constructs. Those are not portable.
If you don't pass by reference but only by value, then how would the calling procedure know if the stream was in a failed state after visiting those functions?
Neither gcc or VC++ compiler on rextester.com will allow it unless it is passed by reference.
#include <iostream>
#include <fstream>
bool write_header( std::ofstream file )
{
// Some operation that puts file in a failed state
// Other stuff
returntrue; // because this was looking at something other than file
}
int main ()
{
std::ofstream out( "my_file.txt" );
write_header( out );
out << "Is this legitimate or not?";
}
Because out is an lvalue, and the copy constructor is deleted, it won't compile.
This is fine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <fstream>
bool write_header( std::ofstream file )
{
// file output operations
returnbool(file); // return true if the stream is not in a failed state
}
int main ()
{
std::ofstream out( "my_file.txt" );
if( write_header( std::move(out) ) )
{
// successfully wrote stuff into the file
}
}
Am I better off learning with GCC rather than VC++?
Learn to use both, one might catch problems with your code the other misses.
Plus, make sure each is as up-to-date as possible. Unless you are maintaining code that needs an older C++ standard using outdated tools can lead to problems.
I use VS17 and Code::Block 17.12 for most of my coding. I am little bit by little bit learning how to do coding by command line and hand crafted make files. It is a bit frustrating at times, but I keep working at improving my skills.
Unless GCC has finally fixed things <random>'s implementation of std::random_device is not truly random. std::random_device::entropy returns zero, indicating GCC is using a pseudo-random number engine.