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

2744 questions
179
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…
76
votes
6 answers

Is `catch(...) { throw; }` a bad practice?

While I agree that catching ... without rethrowing is indeed wrong, I however believe that using constructs like this: try { // Stuff } catch (...) { // Some cleanup throw; } Is acceptable in cases where RAII is not applicable. (Please, don't…
ereOn
  • 2,001
58
votes
7 answers

Why are there so many string classes in the face of std::string?

It seems to me that many bigger C++ libraries end up creating their own string type. In the client code you either have to use the one from the library (QString, CString, fbstring etc., I'm sure anyone can name a few) or keep converting between the…
54
votes
6 answers

Function inadvertently invalidates reference parameter - what went wrong?

Today we found out the cause of a nasty bug that only happened intermittently on certain platforms. Boiled down, our code looked like this: class Foo { map m; void A(const string& key) { m.erase(key); cout << "Erased: "…
Nikolai
  • 561
41
votes
3 answers

What is the role of C++ today?

Currently I'm an IT student and I'm wondering what is still important in C++ today, what for is it used? I completed basic C++ course in my university but I can't imagine where can I use my knowledge and in which direction should I go learning…
hades
  • 645
  • 1
  • 6
  • 5
39
votes
7 answers

Why can't I check whether a mutex is locked?

C++14 seems to have omitted a mechanism for checking whether an std::mutex is locked or not. See this SO question: https://stackoverflow.com/questions/21892934/how-to-assert-if-a-stdmutex-is-locked There are several ways around this, e.g. by…
quant
  • 1,298
38
votes
5 answers

Allow iteration of an internal vector without leaking the implementation

I have a class that represents a list of people. class AddressBook { public: AddressBook(); private: std::vector people; } I want to allow clients to iterate over the vector of people. The first thought I had was…
user120251
35
votes
17 answers

Teaching C++ to first time high school students: Where to draw the line?

I will be mentoring a team of high school students for the FIRST Robotics Competition, most teams here develop[ their robot software using C++. For many of the students on the team this will be their first introduction to programming. I wouldn't…
31
votes
7 answers

How can I learn to write idiomatic C++?

I am a computer science student, and as a result, I was taught C++ as a better version of C with classes. I end up trying to reinvent the wheel whenever a solution to a complex problem is needed, only to find sometime after that, some language…
yati sagade
  • 2,089
  • 2
  • 19
  • 27
24
votes
4 answers

What is the pattern for a safe interface in C++

Note: the following is C++03 code, but we expect a move to C++11 in the next two years, so we must keep that in mind. I'm writing a guideline (for newbies, among others) about how to write an abstract interface in C++. I've read both articles of…
paercebal
  • 1,457
  • 1
  • 11
  • 17
24
votes
7 answers

Are header files actually good?

I find header files to be useful when browsing C++ source files, because they give a "summary" of all the functions and data members in a class. Why do so many other languages (like Ruby, Python, Java, etc.) not have a feature like this? Is this…
Matt Fichman
  • 359
  • 2
  • 6
22
votes
7 answers

How to best protect from 0 passed to std::string parameters?

I have just realized something disturbing. Every time I have written a method that accepts a std::string as a paramater, I have opened up myself to undefined behaviour. For example, this... void myMethod(const std::string& s) { /* Do something…
22
votes
3 answers

Is there a compliance test for C++ compilers?

Is there, somewhere, a freely usable/accessible script, source file, or whatever, that is able to measure the compliance of a given C++ compiler? For example, the Acid3 test for browsers: http://acid3.acidtests.org/ The results I dream of would be a…
paercebal
  • 1,457
  • 1
  • 11
  • 17
21
votes
4 answers

Why doesn't C++ allow you to take the address of a constructor?

Is there a specific reason that this would break the language conceptually or a specific reason that this is technically infeasible in some cases? The usage would be with new operator. Edit: I'm going to give up hope on getting my "new operator" and…
Praxeolitic
  • 1,634
21
votes
9 answers

General programming techniques to speed up coding time

I am preparing for a programming contest where we have to code in C++ and it is all about producing working code in a short time. An example would be to use a macro to get the minimum of two ints or using memsets to initialize arrays (but I was told…
marktani
  • 629
1
2 3
10 11