Questions tagged [c++]

C++ is a general-purpose programming language. Initially, it was designed as an extension to C and has a similar syntax, but it is now a completely different language. Use this tag for questions about code (to be) compiled with a C++ compiler. Use a version-specific tag for questions related to a specific standard revision [C++11], [C++14], [C++17], [C++20], [C++23], or [C++26], etc.

What is C++?

C++ is a (mostly) statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose programming language; not to be confused with C or C++/CLI. It was developed in the early 1980s by Bjarne Stroustrup, initially as a set of extensions to the C programming language. Building on C, C++ improved type-safety and added support for automatic resource management, object orientation, generic programming, and exception handling, among other features.


New to C++?

Whether you are new to programming or coming to C++ from another programming language, it is highly recommended to have a good book from which to learn the language. We keep a detailed list of books.

If you are looking for good compilers, GCC is the most commonly used compiler on Linux and other Unix-like platforms; Clang is the official compiler on Mac and FreeBSD; Microsoft Visual C++ is the most commonly used compiler on Windows. The Intel® C++ Compiler is also commonly used for its optimized numerical computations on Windows, Linux, and Mac.

Turbo-C++ 3.0 is from 1991, extremely outdated, and not recommended. See the previous paragraph for free choices from this millennium.

Join us in chat, where we discuss C++, programming in general, and even other stuff when the sun goes down and boredom creeps in. Don't forget your sense of humor, but keep it civilized.


Tag usage

When posting questions about C++ programming, please make sure to include:

  • Target system and compiler information. This includes the compiler name, version, and settings used to compile.
  • If your question is specific to one particular version of the language, add , , , or . Questions about boost should add the tag . Details about the different versions can be found further below in this wiki.
  • Unless the question explicitly mentions which version of the C++ standard is used, it is assumed that the current version is used. That is, whichever version of ISO 14882 ISO currently lists as active. Please have this in mind when answering or commenting on questions tagged .

Using and together

C and C++ are two distinct and often incompatible languages. Avoid using both tags in the same question unless you have good reasons.

A question should be tagged with only, if:

  • It contains pure C, with no trace of C++, or questions with code that could be either language.
  • The code is compiled with a C compiler.

A question should be tagged with only, if:

  • It contains code with any C++ features. Even though the code may be "C style".
  • The code is compiled with a C++ compiler.

A question should be tagged with both and if it is about:

  • Specific differences between C and C++.
  • Compatibility or porting code between C and C++.
  • C++ code that uses C libraries (for example code using extern "C").

Editing and moderation guidelines for posts with both and tags:

To edit/re-tag/moderate questions with both tags, it is recommended that you have full edit privileges and either a gold or a gold badge.

If you encounter a post with both tags, edit/re-tag it if needed according to the above rules. If you can tell the language by reading the posted code, simply edit tags accordingly. Avoid prompting the user "is it C or C++?" in comments unless the question is truly unclear.

One example of an unclear question is when the user explicitly claims that they are programming in C, but posts code or compiler messages for C++. If so, prompt for clarification and close vote as unclear.

"Either C or C++ is fine" opinions from the OP are a strong indication of a poor or unclear question. Answers may be very different depending on the language picked. Prompt for clarification, close as unclear/too broad until the OP has clarified this.

Be careful about re-tagging questions once there are answers posted, particularly if there are already both C and C++ answers posted. In such cases, the tags should be left alone, since changing them would make posted answers invalid.

Answers with C++ code to a C question that has never been tagged should be deleted as off-topic. Please check the question edit history before flagging/deleting such answers, to verify that the question never had the C++ tag.


C++98

In 1998, the C++ standards committee published the first international standard for C++ ISO/IEC 14882:1998, which would be informally known as C++98.


C++03

In 2003, the C++ Committee responded to multiple problems that were reported with and revised it accordingly. The changed language was dubbed C++03.


C++11

The language standard remained pretty much the same for a long time, but in 2011 a new standard, C++11 (formerly known as C++0x) was published in ISO/IEC 14882:2011. Rather than in a "big bang" approach, it is being rolled out gradually as compilers are supporting the new language features. See Bjarne Stroustrup's C++11 FAQ to see what is new in the language, and check your own compiler's FAQ to see which of those features are currently supported:

A few features that had been under discussion for C++11 have been deferred to the next iteration.


C++14

C++14 is a small extension of C++11. It was approved in August 2014 and released in December of the same year. Previously referred to as C++1y the year of approval was uncertain. Many popular compilers already have some level of C++14 support.


C++17

C++17 adds a few more major features (and several minor ones). It is usually referred to as C++1z or C++17, as technical work on the new standard was completed in March of 2017. The draft standard was approved (unanimously) by the ISO in September 2017 and officially published in December 2017.


C++20

In February 2020 the C++ Standard committee completed work on C++20. It was unanimously approved later that year and got officially published in December 2020.

The following (and more) new features have already been accepted into the C++20:


C++23

The following (and more) new features have already been accepted into the C++23 draft:


C++26

The following (and more) new features have already been accepted into the C++26 draft:


Online compilers

If you want to give C++ a spin, you can try one of the following online compiler services:

  • Coliru (GCC, Clang)
  • ideone.com (GCC, Clang)
  • codepad (GCC)
  • rextester (GCC, Clang, Visual C++)
  • repl.it (GCC)
  • ELLCC (Clang)
  • CodingGround (GCC)
  • C++Shell (GCC)
  • Wandbox (GCC, Clang)
  • Compiler Explorer (GCC, Clang, ICC, Visual C++). Many versions of GCC/Clang for x86, and also for some non-x86 architectures (ARM, ARM64, PPC, and AVR), as well as the latest versions of Microsoft Visual C++. Beside execution (./a.out button), it includes nicely formatted assembler output, even optionally color-highlighting source and assembler lines to help find which source line maps to which assembler line.
  • C++ Insights (Clang) Trans-compiler of code that shows the C++98 code that maps on modern constructs
  • Quick bench (GCC, Clang) Micro benchmarking tool intended to quickly and simply compare the performances of code snippets.


Stack Overflow's C++ FAQ


External FAQs


Other External Resources


Chat Rooms

798869 questions
121
votes
1 answer

Image to ASCII art conversion

Prologue This subject pops up here on Stack Overflow from time to time, but it is removed usually because of being a poorly written question. I saw many such questions and then silence from the OP (usual low rep) when additional information is…
Spektre
  • 49,595
  • 11
  • 110
  • 380
121
votes
7 answers

Convert float to string with precision & number of decimal digits specified?

How do you convert a float to a string in C++ while specifying the precision & number of decimal digits? For example: 3.14159265359 -> "3.14"
Shannon Matthews
  • 9,649
  • 7
  • 44
  • 75
121
votes
5 answers

How are VST Plugins made?

I would like to make (or learn how to make) VST plugins. Is there a special SDK for this? how does one yield a .vst instead of a .exe? Also, if one is looking to make Audio Units for Logic Pro, how is that done? Thanks
jmasterx
  • 52,639
  • 96
  • 311
  • 557
121
votes
12 answers

Incrementing in C++ - When to use x++ or ++x?

I'm currently learning C++ and I've learned about the incrementation a while ago. I know that you can use "++x" to make the incrementation before and "x++" to do it after. Still, I really don't know when to use either of the two... I've never really…
Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
121
votes
7 answers

Cannot find or open the PDB file in Visual Studio C++ 2010

I use Visual Studio 2010 C++ and my project builds without errors but when I run it I get this. I am on Windows XP. 'Shaders.exe': Loaded 'C:\Documents and Settings\User\My Documents\Visual Studio …
user1723768
  • 1,237
  • 2
  • 9
  • 3
121
votes
1 answer

Member initialization while using delegated constructor

I've started trying out the C++11 standard and i found this question which describes how to call your ctor from another ctor in the same class to avoid having a init method or the like. Now i'm trying the same thing with code that looks like…
lfxgroove
  • 3,778
  • 2
  • 23
  • 33
121
votes
8 answers

Best introduction to C++ template metaprogramming?

Static metaprogramming (aka "template metaprogramming") is a great C++ technique that allows the execution of programs at compile-time. A light bulb went off in my head as soon as I read this canonical metaprogramming example: #include…
jwfearn
  • 28,781
  • 28
  • 95
  • 122
121
votes
12 answers

Removing item from vector, while in C++11 range 'for' loop?

I have a vector of IInventory*, and I am looping through the list using C++11 range for, to do stuff with each one. After doing some stuff with one, I may want to remove it from the list and delete the object. I know I can call delete on the pointer…
EddieV223
  • 5,085
  • 11
  • 36
  • 38
120
votes
4 answers

Should C++ function default argument values be specified in headers or .cpp source files?

I am kind of new to C++. I am having trouble setting up my headers. This is from functions.h extern void apply_surface(int, int, SDL_Surface *, SDL_Surface *,SDL_Rect *); And this is the function definition from functions.cpp void apply_surface(int…
yasar
  • 13,158
  • 28
  • 95
  • 160
120
votes
8 answers

Can I list-initialize a vector of move-only type?

If I pass the following code through my GCC 4.7 snapshot, it tries to copy the unique_ptrs into the vector. #include #include int main() { using move_only = std::unique_ptr; std::vector v { move_only(),…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
120
votes
7 answers

Different floating point result with optimization enabled - compiler bug?

The below code works on Visual Studio 2008 with and without optimization. But it only works on g++ without optimization (O0). #include #include #include double round(double v, double digit) { double pow =…
Bear
  • 1,367
  • 2
  • 13
  • 19
120
votes
10 answers

Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization)

Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages? I do know a little bit. I believe it stands for "Resource Acquisition is…
Charlie Flowers
  • 17,338
  • 10
  • 71
  • 88
120
votes
31 answers

What would be C++ limitations compared C language?

Following are the benefits of C++ C++ provides the specific features they are asking about Their C compiler is almost certainly really a C++ compiler, so there are no software cost implications C++ is just as portable as C C++ code can be just as…
anon
120
votes
3 answers

Stack Memory vs Heap Memory

Possible Duplicate: What and where are the stack and heap I am programming in C++ and I am always wondering what exactly is stack memory vs heap memory. All I know is when I call new, I would get memory from heap. If if create local variables, I…
Steveng
  • 1,451
  • 4
  • 15
  • 14
120
votes
2 answers

Why is std::ssize() introduced in C++20?

C++20 introduced the std::ssize() free function as below: template constexpr auto ssize(const C& c) -> std::common_type_t>; A possible…
John Z. Li
  • 1,893
  • 2
  • 12
  • 19
1 2 3
99
100