C++ varies between complier?

I want to know if C++ language varies when you used different compilers or IDE? Or C++ is the exact same syntax and language whatever compiler and IDE you use?

There are several things you need to consider.

1. Which version of C++.
https://en.wikipedia.org/wiki/C%2B%2B#Standardization
If you use features introduced in later standards, then it obviously won't compile on an older compiler.

2. Which compiler (Microsoft, GCC, CLang etc).
All compilers should be capable of compiling conformant C++ programs.
But
- Compilers often have vendor specific extensions.
- Compilers sometimes have bugs.

3. What the standards don't say.
https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior
For example, given a = f() + g();, the order in which f() and g() are called is unspecified. Meaning that if f() and g() had some hidden inter dependency, then you're in for a surprise at some point.

The problem being when unspecified and implementation defined behaviour just 'works' on the first compiler you use. The real problems come when you port to another compiler or upgrade the current compiler, and some of those implicit assumptions raise their heads.

The best place to learn that information is from reading the language standards documents.
I dont follow.... So back to my question, your answer means, C++ differs between compilers and IDE or not?

Can someone tell me if C++ varies between compilers or IDE or not please?
Last edited on
Yes. The answer is yes.

But what salem said is relevant, so I'm pretty much restating what he said. If by "C++", you mean "the C++ standard", then what matters is how compliant the compiler is.
Furthermore, even if two compilers are fully compliant, but have different implementations, for example then the rand() function can produce different results.

Also, the IDE as a UI does not affect this. The compiler (or compiler + linker) is what matters.
Last edited on
A different answer.

1. If you study the language standards, read only books that make no mention of a particular compiler, and focus on writing standard C++ only, then no - the compiler doesn't matter.

2. If you follow "Teach yourself X, using tools from vendor Y in Z hours" kind of books, and take whatever your current compiler lets you get away with as gospel truth, then yes - the compiler matters.

If you choose door number 2, expect pain and suffering whenever you upgrade or change your environment.

there are several answers to this.
standard c++ (for whatever version) is the same across compilers, barring bugs or mistakes.

extended c++ (such as variable sized arrays, void main, union hack, getch, and more) depend on no only the compiler (whether it supports it) but also the compiler's settings (strict standard mode will flag them as error even if supported).

and there are other extensions in the form of IDE (eg, visual studio pulls in GUI for windows tools that are not standard, because that is what the tool is for). These are really libraries but it can mask this, confusing new coders, and microsoft also has a screwed up 'not c++' managed code thing that is loosely based in c++ but radically different as well.

-- there is a lot of c++ code out there -- the web kicked off in the early 90s and ppl been posting code since then. A great deal of code out there in the wild uses nonstandard things, as do any number of books.

if you focus on the standard language it is 100% portable, but such programs will not have {gui, graphics, sound, networking, much more}. Each OS has its own libraries; some libs work across all OS, and many do not. Its fair to say that it is difficult to make portable code in c++. Not impossible, but difficult. The tradeoff is performance and depth. Most portable language either are severely lacking (eg java, missing unsigned ints and operator overloads and more) in tools or extremely slow (python..!) or both.

Assuming you want to use all the tools at your disposal, not just standard c++, the best answer is probably yes, it varies esp across OS barrier
Last edited on
Ok.
I asked because I want to make my own cad drawing program. Yes it is reinventing the wheel, but I just want to do it, both for my learning of C++, which I will use for other things in the future, and also to have my own cad drawing program, even if it may be something extremely simple. In such case, and since you said C++ differs between compilers, which compiler do you suggest? Or it doesnt matter? Also on top of it which libraries do I need? I guess libraries come with compilers ?
Your compiler doesn't matter (so long as it's one of MSVC++, GCC, CLang), nor does your OS matter (so long as it's one of Windows/Linux/xBSD/MacOS).

https://en.wikipedia.org/wiki/Firefox
Think about it, do you think people would rewrite the entire code base from scratch just to make it work at all on another operating system.

The key to portability is to make sure you use only portable libraries, so get into the habit of typing "portable c++ library ...." into google and researching.

For just graphics, then perhaps
https://www.libsdl.org/
https://www.sfml-dev.org/

For a GUI
https://doc.qt.io/qt-5/qtgui-module.html
https://docs.wxwidgets.org/3.0/page_libs.html

For several other things
https://www.boost.org/

OK, it's never quite that simple. You should expect to have to tweak bits here and there to make things work across multiple platforms, but you should be able to avoid complete re-implementation of it.

If you intend to make your program cross-platform, then early and regular tests will help you keep on track.
I only want it to work on windows as , for now, i only make it for my use, and I use windows 10. So I should just simply use MS visual studio that is already in my computer with windows 10 right? Thanks for the detailed answers I appreciate
Also the default version of MS visual studio that came with windows 10 automatically is enough? Or we need a paid upgrade? If so I must chose another compiler
Last edited on
Visual Studio - the best recommendation you can get for a C++ compiler.

C++ changes based on version. Some of the versions are:

C++98, C++11, C++17, C++20

The numbers correlate to the year released.

Each version introduces new features, and sometimes removes a few features. For the most part, the language is mostly the same throughout all the versions. You usually don't have to worry about features that may or may not be present in the version you're using for beginner to intermediate coding.

With Visual Studio, you can control which version of C++ you want to code in.
Ok. And the default, free version that already came with my Windows 10 would be enough to develop a very simple drawing program by myself for my own use? All I want is to create a drawing program that I can draw lines, circles ellipses rectangles with dimensions, angles etc.... Whatever basic things I can make. This is mostly for my own learning of C++, and still to have a basic drafting program on my own.

I saw that on microsoft website it has upgrades such as MS VIsual Studio Professional or MS Visual Studio Enterprise. For my purpose they are not needed?
The free version of Visual Studio is just fine for that. It would be likely easier to create a program like you've described using a different language, like C#. An external library will be greatly needed for a program like you're describing, which means you won't be practicing core C++ as much as you would be with other programs you could try out.
Thanks, and since you are familiar with my situation now, although we deviated form main question, do you know the names of such libraries?
https://stackoverflow.com/questions/1610210/c-graphic-drawing-library

Google will have a lot of libraries for you to look at :)
You can always stick with standard C++ and generate some SVG graphics to view in your web browser.
https://www.w3schools.com/graphics/svg_intro.asp

After running, open the file plot.svg in a web browser.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

template <typename T> void write( ostream &out, const string &item, const vector< pair<string,T> > &V, const vector< pair<string,string> > &W = {} )
{
   const char q = '\"';
   out << "<" << item;
   for ( auto pr : V ) out << " " << pr.first << "=" << q << pr.second << q;
   for ( auto pr : W ) out << " " << pr.first << "=" << q << pr.second << q;
   out << " />\n";
}

class Svg
{
   ofstream out;
   string colour = "red";
   string fill   = "green";
   double width  = 4;

public:
   Svg( string filename, int width = 1000, int height = 1000, string background = "#FFFFFF" )
   {
      out.open( filename );
      out << "<?xml version=\"1.0\" standalone=\"no\"?>\n"
          << "<svg "
          << "xmlns=\"http://www.w3.org/2000/svg\" "
          << "width=\"" << width << "\" height=\"" << height << "\" "
          << "style=\"background: " << background << "\" "
          << ">\n";
   }


   ~Svg()
   {
      out << "</svg>";
      out.close();
   }


   void setcolour( const string &s ){ colour = s; }
   void setfill  ( const string &s ){ fill   = s; }
   void setwidth (       double  w ){ width  = w; }

   void line( double x1, double y1, double x2, double y2 )
   {
      write<double>( out, "line", { { "x1", x1 }, { "y1", y1 }, { "x2", x2 }, { "y2", y2 }, { "stroke-width", width } },
                                  { { "stroke", colour }, { "fill", fill } } );
   }

   void rectangle( double x, double y, double w, double h )
   {
      write<double>( out, "rect", { { "x", x }, { "y", y }, { "width", w }, { "height", h }, { "stroke-width", width } },
                                  { { "stroke", colour }, { "fill", fill } } );
   }

   void circle( double r, double cx, double cy )
   {
      write<double>( out, "circle", { { "r", r }, { "cx", cx }, { "cy", cy }, { "stroke-width", width } },
                                  { { "stroke", colour }, { "fill", fill } } );
   }
};


int main()
{
   double PI = 3.14159;
   string filename = "plot.svg";
   double width = 800, height = 800;

   double r0 = 10, dr = 5;
   double theta0 = 0, dtheta = 20;
   int npoints = 70;

   Svg svg( filename, width, height );
   svg.setcolour( "red" );
   svg.setfill( "blue" );
   svg.setwidth( 2 );
   for ( int i = 0; i < npoints; i++ )
   {
      double r = r0 + i * dr;
      double theta = theta0 + i * dtheta;
      double x = 0.5 * width  + r * cos( theta * PI /180.0 );
      double y = 0.5 * height - r * sin( theta * PI /180.0 );
      double radius = 0.03 * r;
      svg.circle( radius, x, y );
   }

   svg.setcolour( "black" );
   svg.setfill( "orange" );
   svg.setwidth( 4 );
   svg.rectangle( 0.0, 0.0, 0.2 * width, 0.2 * height );

   svg.setcolour( "violet" );
   svg.line( 0.2 * width, 0.2 * height, 1.0 * width, 1.0 * height );
}
Last edited on
Topic archived. No new replies allowed.