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

learn more… | top users | synonyms

1
vote
1answer
165 views

How to make sure that the destructor cannot throw an exception?

I know that we can specify the compiler that a function is not throwing an exception by using noexcept specifier but noxecept functions can still throw exceptions . So, how to prevent destructor from ...
1
vote
1answer
215 views

How to avoid violating one definition rule in this case?

We have an existing Mutex C++ class. It has a debug feature to track the owner thread id. So it has a member variable to store this, but because the original developer doesn't want to waste memory, ...
4
votes
2answers
540 views

Const means Thread-safe?

I was put onto this video: http://channel9.msdn.com/posts/C-and-Beyond-2012-Herb-Sutter-You-dont-know-blank-and-blank By http://codereview.stackexchange.com/users/39810/glampert Because I asked ...
2
votes
2answers
131 views

Free Standing Functions in Global Namespace

When writing non-member, free functions, can they be placed in the global namespace so long as the signature specifies a namespace-scoped object? For example, in the code below, is "Example 2" ...
4
votes
1answer
128 views

Convince Windows SmartScreen and web browser that my application is safe?

I am developing a Free and Open-Source software (C++). When I download my own application from a web link, Microsoft Smart Screen and Google Chrome are getting in the way since it comes from and ...
2
votes
2answers
105 views

Typedefs to convey relations between classes

I'm wondering if the following use of typedefs is any good practice, or if there are any downsides to it. Basically I have alot of "data"-structs, which are intended to be used in (globally unique) ...
3
votes
2answers
278 views

How to iterate through all permutations of valid links between nodes

Imagine a 2D area, with a number (array) of nodes (or points) defined within it, in arbitrary (but known) positions (integer x,y coordinates), like this: From there I want to be able to, ...
1
vote
2answers
108 views

Logging function names on systems with few (memory and space) resources

Our logging module has a buffer that often gets full and we are searching for a solution to shorten our logs, without making any compormise in performance. The code is written in C++ and currently we ...
0
votes
5answers
572 views

Is a makefile really needed

I have been programming in c/c++ for a while now, and I have never once used a makefile. I know that it is supposed to be useful when you are dealing with a large project, but I have always been able ...
0
votes
0answers
113 views

Polymorphic template container: shared_ptr vs reference_wrapper

Assuming we have two classes: class A { ... } class B : public A { ... } Would it be better to write std::deque<shared_ptr<A> > container; or ...
0
votes
1answer
62 views

Perrin sequence help c++

Hey everyone I am trying to write a recursive function for a perrin sequence where it returns the nth term that a user enters which is: P(0) = 3, P(1) = 0, P(2) = 2, and P(n) = P(n − 2) + P(n − 3) ...
61
votes
11answers
8k 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 ...
3
votes
2answers
810 views

How do you usually compile C/C++ source code ? (Large code bases) [closed]

It's been quite some times since I began learning C and C++ but I've been very limited only to the Windows platform and the Visual Studio environment. Recently, I wanted to look into some open source ...
3
votes
1answer
238 views

How can I use the “Non-Member Functions Improve Encapsulation” pattern from C#?

In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function: ...
0
votes
1answer
266 views

C++ Templates where the type is a shared_ptr

When creating template classes in C++, if the type upon which the template will be specialized is intended to be a shared_ptr type, is it better to make T a shared_ptr type or make T a non-pointer ...
0
votes
2answers
115 views

Organizing C++ Components [closed]

In my current company we've brought the discussion about how to organize most of our different C++ components considering the following requirements: There might be interdependencies between ...
2
votes
1answer
267 views

An embedded C developer wishing to interface some code with a web page

Okay, so as the title says, I'm an embedded C/C++ developer. Right now, I'm working on a project that involves a client/server C application. The server side of the application is running on ...
-1
votes
5answers
2k views

Why Java is a factor 2-3 slower than equivalent C++ program? [closed]

I know there is an opinion that programs written in Java and running under JVM as as fast as C++ programs, after introducing just-in-time (JIT). I see many cases when Java is terribly slow for simple ...
0
votes
1answer
125 views

Is it not direct usage of primitive pointer types form of Primitive Obsession?

Using primitive data types to represent domain ideas is code smell called Primitive Obsession. For example using string data type to represent human name - it is recomended to avoid this by defining ...
1
vote
1answer
100 views

Should factory make context or concrete strategies objects in strategy-factory pattern?

In a typical strategy pattern class Strategy { public: virtual int execute() const = 0; } class StrategyA : public Strategy { public: int execute() const override; } class StrategyB : ...
2
votes
1answer
233 views

Well defined mathematical operations for bearing (angle) class

I have written a class to represent bearings (angles with a nautical theme, and a specific normalisation range). In the program, it is necessary to perform some mathematical operations on them, so ...
1
vote
0answers
134 views

General design choices. How do you decide?

I have a project in C that I'm looking to convert to C++. The project does white box hardware testing of a device (actually many similar devices). In this case the device has two processors. Each ...
2
votes
1answer
330 views

Tips for understanding big project fast? [duplicate]

I've got a job and many new things to be learned. The first thing is to understand big project, writtened mostly in C++, using Visual Studio. So, I see a statement, and I want to find its definition. ...
0
votes
1answer
157 views

How shall I handle event loop exceptions?

What is the best practice for handling exceptions thrown from event handlers/listeners in a event loop? For example: class EventLoop { public: void start(); //create a thread which calls run(); ...
1
vote
1answer
205 views

Why does this algorithm work in O(n m)?

This is from a blog post on Codeforces. I couldn't really understand why the editorialist goes on to claim that this code works in O(n m) This is a graph problem, where we are supposed to find the ...
1
vote
2answers
211 views

Why does this implementation of Dijkstra's algorithm work in O(n^2)?

Here is the code I use for implementing Dijkstra's algorithm. Consider a graph with n vertices and m edges. Shouldn't it run in O(n^2 m) ? Someone may say that there are n vertices and each edge gets ...
-3
votes
1answer
262 views

If-else in C++ Programming Language [closed]

I am a little bit confused about this code. Why does the else block get executed? Please explain it. int a=10,b=20; char x=1,y=0; if(a,b,x,y){ cout << "A" << endl; }else{ cout ...
1
vote
1answer
211 views

Using streams to connect programming languages

I'm trying to run a computation-heavy program that creates an image on a website. Is it possible to compute in C++ and have an output stream that connects to an input stream in Node.js to display an ...
1
vote
0answers
67 views

Maximizing reuse out of a function primitive in CUDA

I'm working on the implementation of a few related algorithms in CUDA, all of which require a primitive that we'll call f(). The related algorithms can't simply call f though, as they require f to ...
2
votes
1answer
213 views

Genetic Algorithm's Tournament Selection limit to be selected

I started working again on a Genetic Algorithm and i'm trying a lot of operators and ways of selection. When I made the Tournament Selection , I noticed that it gets really easy to always get the top ...
2
votes
1answer
222 views

Insertion sorting Algorithm comparison

I recently came across sorting techniques and of particular, 'insertion sorting'. Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below). ...
1
vote
1answer
175 views

C++ Template Metaprogramming and Call By Need Evaluation

I recently wrote a fairly complex C++ meta function that boils down to: template <size_t N, typename val> struct Rec { using type = typename std::conditional<N == 0, val, ...
4
votes
3answers
495 views

Should I try it straight or should I first check if it can be done?

I have a set of items, and each item in the set must be unique. Item are composed from multiple properties and each property of each item can be changed. But after each change every item in the set ...
4
votes
1answer
303 views

What is the status of @ and $ characters in C++?

C++ syntax uses almost all printable ASCII characters and some of them in several contexts. Only two unused are @ and $ characters. Why they are not used? Are they reserved for some "meta" purpose?
3
votes
4answers
526 views

Are `switch` statements generally used wrong? [closed]

I see most developers using switch statements with breaks in each and every case. Is this usage of switch statements inappropriate since a simple set of if-else statements would suffice? Is it OK for ...
3
votes
0answers
241 views

How do you pronounce 'char' keyword? [closed]

So, with the possibility of this question being closed due to being non-productive as in the case of other pronunciation questions, I take the risk and ask it just because i have heard it said both ...
1
vote
2answers
132 views

Class as resource handle & Bad Copy

I am reading Bjarne Stroustrup's book titled "A tour of C++". The following section is confusing to me: When a class is a resource handle – that is, when the class is responsible for an object ...
-1
votes
1answer
96 views

What are some hashmaps we can easily implement? [duplicate]

At many places, I read that we can use HashMap here for O(1) search. Actually, I want to ask how I can implement easy hashmaps which can satisfy this property. Can anyone tell few hashmaps including ...
1
vote
1answer
220 views

Constructor vs casting operator

I'm programming a library (so I have complete access to all the mentioned classes). Two classes (A and B) are essentially the same and differ only by their implementation, so they can be easily ...
5
votes
1answer
201 views

Designing interfaces to hardware

I am writing a library in C++ that is used by customers to interface with hardware. There are many different devices that I need to support. For the sake of simplicity, assume I have 2 Widgets, ...
8
votes
5answers
2k views

How to structure a loop that repeats until success and handles failures

I am a self-taught programmer. I started programming about 1.5 years ago. Now I have started to have programming classes in school. We have had programming classes for 1/2 year and will have another ...
9
votes
7answers
1k views

Will a profound knowledge of C++ help you in learning other languages faster/easier? [closed]

Is there a possibility that someone who has learned all the key aspects of C++ and knows the fundamentals of the language very well, will learn other languages (such as, Python, Perl, Java) faster and ...
-1
votes
2answers
588 views

What is the simplest and universal algorithm for parsing C++ code? [closed]

I need to do some project specific automatic checking of source codes written in C++ Limitations: Algorithm and its implementation should be simple, easily maintainable, extendable and ...
3
votes
2answers
316 views

Is it always safe to shift bool values?

I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&: /// @param code identifies the command. Only the lower 16 bits of are being processed ...
0
votes
3answers
84 views

Defining resource allocation in method documentation

I want to write a library with "no resource leak guarantee" feature. So I shall know whether each method allocates any resource or not. It may change the code. For example: void foo() { ...
4
votes
2answers
301 views

Needs (in principle) C++ parenthesis around if statement condition?

In current C++ when body of if statements contain only one command then: Parenthesis around if condition are mandatory but block are optional. So, both examples are OK: if ( condition ) { return 0; ...
2
votes
2answers
624 views

Use of raw pointer of std::vector

I am currently maintaining a code that makes liberal use of raw pointers where std::vector would be a better option. Worse, these pointers are not always properly handled (when run using Intel ...
3
votes
2answers
188 views

How to deal with historic exception specifications in maintained C++ code

Today, I discovered a few exception specifications in legacy code I've to maintain, when Cppcheck 1.68 ([Inconclusive] but correctly) pointed out that the specified exceptions are not handled in ...
1
vote
0answers
128 views

Requesting information regarding the use of MUI vs. the use of GetText for i18n/L10n of Windows desktop applications

I have spent the past two days investigating which technology I will use to support translations of text strings (as part of an overall internationalization migration) for an archaic (VC6) Windows ...
3
votes
3answers
367 views

Where should I place a typedef when used in method signatures in C++?

I'm using an Optional class quite similar to that of boost. For semantic reasons, I switched an attribute of the same (structured) type in some class definitions (and therefore also in method ...