Questions tagged [c++]

Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.

[Above excerpt quoted from Wikipedia.]

New to C++?

Welcome! Whether you are new to programming or are coming to C++ from another programming language, it is highly recommended to have a good book from which to learn the language.

If you are looking for a good compiler, g++ is the most commonly used compiler on Linux and other platforms, while Microsoft Visual C++ is the most commonly used on Windows. We also have a list of toolsets.

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.

C++0x

The language standard has remained pretty much the same for a long time, but the new standard C++0x has now been defined. Rather than a "big bang" approach, it is being rolled out gradually as compilers are supporting the new language features. See the C++0x FAQ to see what is new in the language, and check your own compilers FAQs to see which of those features are currently supported.

Have a Question?

When you ask a question, be sure to include any relevant source code. Try to keep the code as minimalist as possible while still reproducing the problem; often the problem will be found during the process. Try to make sure that the source code compiles, if possible. However, if there are any compiler errors, be sure to indicate:

  • which compiler you are using
  • exactly what the errors are and
  • on which lines they occur (mark the lines with comment)

Stack Overflow's C++ FAQ

We've recently started an effort to create a list of C++ questions frequently asked on Stack Overflow. You can reach them using the [c++-faq] tag.

External FAQs

On C++0x

Other External Resources

Third party code

General Posts

2707 questions
252
votes
11 answers

Why do many exception messages not contain useful details?

It seems there is a certain amount of agreement that exception messages should contain useful details. Why is it that many common exceptions from system components do not contain useful details? A few examples: .NET List index access…
Martin Ba
  • 7,578
  • 7
  • 34
  • 56
228
votes
8 answers

When do you use float and when do you use double

Frequently, in my programming experience, I need to make a decision whether I should use float or double for my real numbers. Sometimes I go for float, sometimes I go for double, but really this feels more subjective. If I would be confronted to…
Jakub Zaverka
  • 2,429
  • 2
  • 14
  • 8
198
votes
40 answers

My Dad is impatient with the pace of my learning to program. What do I do?

So my Dad bought me 5 books on programming (C++, Java, PHP, Javascript, Android) about a month ago. He's an architect and he knows NOTHING about programming. He bought me them because I told him programming was fun and I wanted to learn it. As you…
David
  • 461
  • 2
  • 6
  • 11
186
votes
3 answers

Is C++11 Uniform Initialization a replacement for the old style syntax?

I understand that C++11's uniform initialization solves some syntactical ambiguity in the language, but in a lot of Bjarne Stroustrup's presentations (particularly those during the GoingNative 2012 talks), his examples primarily use this syntax now…
void.pointer
  • 4,983
  • 8
  • 30
  • 40
171
votes
5 answers

What's the reason for not using C++17's [[nodiscard]] almost everywhere in new code?

C++17 introduces the [[nodiscard]] attribute, which allows programmers to mark functions in a way that the compiler produces a warning if the returned object is discarded by a caller; the same attribute can be added to an entire class type. I've…
Christian Hackl
  • 1,884
  • 2
  • 11
  • 12
169
votes
18 answers

Is there any reason to use C++ instead of C, Perl, Python, etc.?

As a Linux (server side) developer, I don't know where and why should I use C++. When I'm going for performance, the first and last choice is C. When "performance" isn't the main issue, programming languages like Perl and Python would be good…
Ehsan
  • 853
  • 3
  • 8
  • 9
163
votes
12 answers

When to use C over C++, and C++ over C?

I've been introduced to Computer Science for a little over a year now, and from my experience it seems that C and C++ are both considered to be "ultrafast" languages, whereas others such as Python and such scripting languages are usually deemed…
Dark Templar
  • 6,223
  • 16
  • 46
  • 46
147
votes
15 answers

Why did memory-managed languages like Java, Javascript, and C# retain the `new` keyword?

The new keyword in languages like Java, Javascript, and C# creates a new instance of a class. This syntax seems to have been inherited from C++, where new is used specifically to allocate a new instance of a class on the heap, and return a pointer…
Channel72
  • 2,475
  • 5
  • 27
  • 28
147
votes
14 answers

Does auto make C++ code harder to understand?

I saw a conference by Herb Sutter where he encourages every C++ programmer to use auto. I had to read C# code some time ago where var was extensively used and the code was very hard to understand—every time var was used I had to check the return…
Mircea Ispas
  • 1,603
  • 2
  • 11
  • 7
144
votes
11 answers

Should I stop using the term C/C++?

I understand C and C++ are different languages but when I was learning C++ I was always told that C is a subset of C++ or C++ is C with classes. And that was quite true until the appearance of C++x0, C++11 (or the modern C++ 11/14/17 in general). In…
rkachach
  • 1,219
  • 2
  • 9
  • 9
136
votes
16 answers

Why do people use C if it is so dangerous?

I am considering learning C. But why do people use C (or C++) if it can be used 'dangerously'? By dangerous, I mean with pointers and other similar stuff. Like the Stack Overflow question Why is the gets function so dangerous that it should not be…
Tristan
  • 1,295
  • 2
  • 9
  • 11
134
votes
8 answers

What does C++ do better than D?

I have recently been learning D and am starting to get some sort of familiarity with the language. I know what it offers, I don't yet know how to use everything, and I don't know much about D idioms and so on, but I am learning. I like D. It is a…
Anto
  • 11,157
  • 13
  • 67
  • 103
129
votes
8 answers

Is it better to document functions in the header file or the source file?

In languages that distinguish between a "source" and "header" file (mainly C and C++), is it better to document functions in the header file: (pilfered from CCAN) /** * time_now - return the current time * * Example: * printf("Now is %lu…
Joey Adams
  • 5,535
  • 3
  • 30
  • 34
119
votes
11 answers

What backs up the claim that C++ can be faster than a JVM or CLR with JIT?

A reoccurring theme on SE I've noticed in many questions is the ongoing argument that C++ is faster and/or more efficient than higher level languages like Java. The counter-argument is that modern JVM or CLR can be just as efficient thanks to JIT…
Anonymous
  • 3,546
  • 2
  • 25
  • 26
112
votes
7 answers

Why declare a variable in one line, and assign to it in the next?

I often see in C and C++ code the following convention: some_type val; val = something; some_type *ptr = NULL; ptr = &something_else; instead of some_type val = something; some_type *ptr = &something_else; I initially assumed that this was a…
Jonathan Sterling
  • 1,606
  • 2
  • 12
  • 14
1
2 3
99 100