Questions tagged [c++]

C++ is a general-purpose programming language. It was originally 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] or [C++23], 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 definitely 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 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 that 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 is a strong indication of a poor or unclear question. Answers may be very different depending on 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 to C++11. It was approved in August 2014 and released in December of the same year. Previously referred to as C++1y as 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 of 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:


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

726181 questions
25552
votes
26 answers

Why is processing a sorted array faster than processing an unsorted array?

Here is a piece of C++ code that shows some very peculiar behavior. For some strange reason, sorting the data miraculously makes the code almost six times faster: #include #include #include int main() { //…
GManNickG
  • 459,504
  • 50
  • 465
  • 534
9401
votes
26 answers

What is the "-->" operator in C/C++?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. Here's the code: #include int main() { …
GManNickG
  • 459,504
  • 50
  • 465
  • 534
4235
votes
1 answer

The Definitive C++ Book Guide and List

This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programming languages, which are often picked up on the go from tutorials found on the Internet, few are able to…
grepsedawk
  • 5,653
  • 5
  • 21
  • 21
3485
votes
40 answers

What are the differences between a pointer variable and a reference variable in C++?

I know references are syntactic sugar, so code is easier to read and write. But what is the difference between a pointer variable and a reference variable?
prakash
  • 55,001
  • 25
  • 87
  • 114
3170
votes
11 answers

What does the explicit keyword mean?

What does the explicit keyword mean in C++?
Skizz
  • 64,439
  • 10
  • 63
  • 105
3140
votes
78 answers

How do I iterate over the words of a string?

I'm trying to iterate over the words of a string. The string can be assumed to be composed of words separated by whitespace. Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give…
Ashwin Nanjappa
  • 68,458
  • 72
  • 198
  • 283
2886
votes
39 answers

Why is "using namespace std;" considered bad practice?

I've been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous…
akbiggs
  • 29,543
  • 5
  • 23
  • 35
2767
votes
27 answers

How do you set, clear, and toggle a single bit?

How do you set, clear, and toggle a bit?
JeffV
  • 47,302
  • 31
  • 96
  • 120
2703
votes
9 answers

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast C-style cast (type)value Function-style cast type(value) How does one decide which to use in which specific cases?
e.James
  • 109,080
  • 38
  • 170
  • 208
2602
votes
30 answers

What is the difference between #include and #include "filename"?

In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include statement, as follows? #include #include "filename"
quest49
  • 42,302
  • 4
  • 18
  • 14
2323
votes
10 answers

Why are elementwise additions much faster in separate loops than in a combined loop?

Suppose a1, b1, c1, and d1 point to heap memory, and my numerical code has the following core loop. const int n = 100000; for (int j = 0; j < n; j++) { a1[j] += b1[j]; c1[j] += d1[j]; } This loop is executed 10,000 times via another outer…
Johannes Gerer
  • 24,320
  • 5
  • 24
  • 33
2284
votes
8 answers

What is The Rule of Three?

What does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
2255
votes
7 answers

What are the basic rules and idioms for operator overloading?

Note: The answers were given in a specific order, but since many users sort answers according to votes, rather than the time they were given, here's an index of the answers in the order in which they make the most sense: The General Syntax of…
sbi
  • 204,536
  • 44
  • 236
  • 426
2138
votes
5 answers

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Does the idiom change when C++11 is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is…
GManNickG
  • 459,504
  • 50
  • 465
  • 534
2023
votes
8 answers

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

C++11 introduced a standardized memory model, but what exactly does that mean? And how is it going to affect C++ programming? This article (by Gavin Clarke who quotes Herb Sutter) says that, The memory model means that C++ code now has a…
Nawaz
  • 327,095
  • 105
  • 629
  • 812
1
2 3
99 100