Questions tagged [c++]
Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
2,768
questions
253
votes
12
answers
30k
views
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 ...
230
votes
8
answers
299k
views
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 ...
197
votes
40
answers
21k
views
My Dad is impatient with the pace of my learning to program. What do I do? [closed]
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 ...
187
votes
3
answers
80k
views
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 ...
179
votes
5
answers
76k
views
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 ...
169
votes
18
answers
201k
views
Is there any reason to use C++ instead of C, Perl, Python, etc.? [closed]
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 ...
163
votes
12
answers
521k
views
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 ...
152
votes
14
answers
48k
views
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—...
149
votes
15
answers
31k
views
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 ...
145
votes
11
answers
24k
views
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, ...
135
votes
16
answers
50k
views
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 ...
134
votes
8
answers
93k
views
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 ...
133
votes
8
answers
107k
views
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 ...
120
votes
11
answers
55k
views
What backs up the claim that C++ can be faster than a JVM or CLR with JIT? [closed]
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 ...
114
votes
5
answers
14k
views
Does a `long` ban make sense?
In today's cross-platform C++ (or C) world we have:
Data model | short | int | long | long long | pointers/size_t | Sample operating systems
...
LLP64/IL32P64 16 32 32 64 ...
113
votes
7
answers
64k
views
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 = &...
110
votes
13
answers
20k
views
Should we avoid language features that C++ has but Java doesn't?
Suppose I am limited to use C++ by the environment in the project. Is it good to prevent the use of some language features that C++ has but Java doesn't have (e.g.: multiple inheritance, operator ...
107
votes
10
answers
22k
views
Is it ok to use meta-programming even though not all of my colleagues understand it?
I employ a lot of meta-programming to avoid repetitive tasks and build safer-to-use abstractions.
I recently moved to a new job where I am working in a larger team and this worries some of my ...
104
votes
14
answers
73k
views
Why are most browsers developed in C++ [closed]
It seems like most of common web browsers (Firefox, Chrome, Safari) are developed using C++. Whys is that so?
101
votes
14
answers
26k
views
Is it bad practice to write code that relies on compiler optimizations?
I've been learning some C++, and often have to return large objects from functions that are created within the function. I know there's the pass by reference, return a pointer, and return a ...
100
votes
3
answers
134k
views
Why is Python written in C and not in C++?
In Python's tutorial one can read that Python's original implementation is in C;
On the other hand, the Python implementation, written in C, (...)
I'm very curious why was Python written in C and ...
97
votes
5
answers
63k
views
Why do we need to put private members in headers?
Private variables are a way to hide complexity and implementation details to the user of a class. This is a rather nice feature. But I do not understand why in c++ we need to put them in the header of ...
96
votes
5
answers
33k
views
Why are there no package management systems for C and C++? [closed]
There are some programming languages for which exist a package management system:
CTAN for TeX
CPAN for Perl
Pip & Eggs for Python
Maven for Java
cabal for Haskell
Gems for Ruby
npm for NodeJS
...
95
votes
8
answers
71k
views
Is modern C++ replacing C#? Is Microsoft pushing developers to adopt C++? [closed]
I hear about modern C++ popularity and some talks about migrating back to C++ from C# or other C-like languages.
I know about C++11 features but I would like to hear your experiences, especially from ...
88
votes
11
answers
13k
views
Did the developers of Java consciously abandon RAII?
As a long-time C# programmer, I have recently come to learn more about the advantages of Resource Acquisition Is Initialization (RAII). In particular, I have discovered that the C# idiom:
using (var ...
84
votes
13
answers
38k
views
Why would it ever be possible for Java to be faster than C++?
Sometimes Java outperforms C++ in benchmarks. Of course, sometimes C++ outperforms.
See the following links:
http://keithlea.com/javabench/
http://blog.dhananjaynene.com/2008/07/performance-...
82
votes
12
answers
53k
views
What's the benefit of object-oriented programming over procedural programming?
I'm trying to understand the difference between procedural languages like C and object-oriented languages like C++. I've never used C++, but I've been discussing with my friends on how to ...
81
votes
11
answers
20k
views
Why is base-for-all-objects discouraged in C++
Stroustrup says "Don’t immediately invent a unique base for all of your classes (an Object class). Typically, you can do better without it for many/most classes." (The C++ Programming Language Fourth ...
80
votes
20
answers
8k
views
Is it ok to replace optimized code with readable code?
Sometimes you run into a situation where you have to extend/improve some existing code. You see that the old code is very lean, but it's also difficult to extend, and takes time to read.
Is it a good ...
76
votes
6
answers
57k
views
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 ...
75
votes
8
answers
107k
views
Using scoped enums for bit flags in C++
An enum X : int (C#) or enum class X : int (C++11) is a type that has a hidden inner field of int that can hold any value. In addition, a number of predefined constants of X are defined on the enum. ...
71
votes
9
answers
58k
views
std::shared_ptr as a last resort?
I was just watching the "Going Native 2012" streams and I noticed the discussion about std::shared_ptr. I was a bit surprised to hear Bjarne's somewhat negative view on std::shared_ptr and his comment ...
70
votes
19
answers
5k
views
Why should I care about micro performance and efficency?
Many questions and answers on the C/C++ pages, specifically or indirectly discuss micro performance issues (such is the overhead of an indirect vs direct vs inline function), or using an O(N2) vs O(...
69
votes
7
answers
62k
views
Why is there no 'finally' construct in C++?
Exception handling in C++ is limited to try/throw/catch. Unlike Object Pascal, Java, C# and Python, even in C++ 11, the finally construct has not been implemented.
I have seen an awful lot of C++ ...
69
votes
8
answers
68k
views
When NOT to use virtual destructors?
I believed I searched many times about virtual destructors, most mention the purpose of virtual destructors, and why you need virtual destructors. Also I think in most cases destructors need to be ...
68
votes
10
answers
20k
views
Why do languages require parenthesis around expressions when used with "if" and "while"?
Languages like C, Java, and C++ all require parenthesis around an entire expression when used in an if, while, or switch.
if (true) {
// Do something
}
as opposed to
if true {
// Do ...
66
votes
13
answers
21k
views
Is it reasonable to null guard every single dereferenced pointer?
At a new job, I've been getting flagged in code reviews for code like this:
PowerManager::PowerManager(IMsgSender* msgSender)
: msgSender_(msgSender) { }
void PowerManager::SignalShutdown()
{
...
64
votes
10
answers
72k
views
Is better Show() + Hide() or SetVisible(bool visible)?
What is better and why? (From interface-design point of view) :
a) To have two Show() and Hide() functions
b) To have one SetVisible(bool visible) function
EDIT: For example some object have ...
64
votes
10
answers
20k
views
I never use pointers in my C++ code. Am I coding C++ wrong? [closed]
This question may sound strange to you, but I am learning C++ all by myself. I have nobody whom I could ask for mentoring and I would be very glad for some advice.
I have started recently to program ...
64
votes
3
answers
49k
views
Are header-only libraries more efficient?
Assumptions
One of the advantages of header-only libraries for C++ is that they do not need to be compiled separately.
In C and C++ inline makes sense only if the function is defined in a header ...
63
votes
10
answers
12k
views
Readability versus maintainability, special case of writing nested function calls
My coding style for nested function calls is the following:
var result_h1 = H1(b1);
var result_h2 = H2(b2);
var result_g1 = G1(result_h1, result_h2);
var result_g2 = G2(c1);
var a = F(result_g1, ...
63
votes
10
answers
12k
views
Is it bad practice to use a C++ compiler just for function overloading?
So I am working on a software design using C for a certain processor. The tool-kit includes the ability to compile C as well as C++. For what I am doing, there is no dynamic memory allocation ...
63
votes
9
answers
27k
views
Is it good practice to rely on headers being included transitively?
I'm cleaning up the includes in a C++ project I'm working on, and I keep wondering whether or not I should explicitly include all headers used directly in a particular file, or whether I should only ...
63
votes
13
answers
10k
views
Philosophy behind Undefined Behavior
C\C++ specifications leave out a large number of behaviors open for compilers to implement in their own way. There are a number of questions that always keep getting asked here about the same and we ...
61
votes
12
answers
54k
views
Are utility classes with nothing but static members an anti-pattern in C++?
The question Where should I put functions that are not related to a class has sparked some debate over whether it makes sense in C++ to combine utility functions in a class or just have them exist as ...
59
votes
11
answers
14k
views
Why does C++ have 'undefined behaviour' (UB) and other languages like C# or Java don't?
This Stack Overflow post lists a fairly comprehensive list of situations where the C/C++ language specification declares as to be 'undefined behaviour'. However, I want to understand why other modern ...
59
votes
17
answers
10k
views
What programming problems are best solved by using pointers? [closed]
Well, I basically understand how to use pointers, but not how best to use them in order to do better programming.
What are good projects or problems to resolve involving the use of pointers so I can ...
59
votes
9
answers
22k
views
Explanation on how "Tell, Don't Ask" is considered good OO
This blogpost was posted on Hacker News with several upvotes. Coming from C++, most of these examples seem to go against what I've been taught.
Such as example #2:
Bad:
def check_for_overheating(...
58
votes
7
answers
19k
views
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 ...
58
votes
11
answers
16k
views
A modern review of Java [closed]
I've been programming for a few years and I began in Java, and in my time I've found many different sources claiming Java to be an inferior language in some way or another. I'm well aware that each ...