简体   繁体   中英

Differences between C++ compilers

While working on my assignment for data structures today, I came across a few errors I had to address that were very new to me. For example, the following:

struct Node
{
  // data and stuff

 Node(const T& d = T{}, Node * const p = nullptr, 
     Node * const n = nullptr, unsigned int f = 0)
     : data(d), prev(p), next(n), freq(f) {}

  // other stuff
};

This ran perfectly fine in visual studio, but not however in g++. I had to change nullptr to 0, even though visual studio wanted me to use nullptr!

Another incident using the school's compiler gave me the following message:

warning:  extended initializer lists only available with -std=c++0x

So naturally I went ahead and added this to my makefile.

This got me wondering, how often will I see errors result from simple differences in compilers, given that I will be writing more complex programs in the future? Is visual studio not backwards compatible with older compilers? Can I change settings? Just looking for general knowledge of things I should be aware of when it comes to using various compilers.

g++ will by default compile in C++03 mode, which was the C++ standard before C++11. To tell g++ that your code is in fact C++11 code, you have to use the -std=c++11 compiler switch. For example:

g++ -std=c++11 -o test test.cc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM