Questions tagged [c++11]

C++11 is the name of the C++ standard, approved in 2011. It replaces the previous C++03 standard, adding various core language changes and fixes, and an improved and expanded standard library.

Filter by
Sorted by
Tagged with
1
vote
2answers
202 views

Introduce code standard into old code

Our main product is written in C++ MFC and follows the same code standard as MFC. Now we will start to develop new components, and think about whether we should continue to use the same outdated ...
0
votes
0answers
104 views

multithreading - waiting on a condition without using locks (c++11)

i've been wondering about it for a while now but never found any answers. is it possible to use something like a condition variable without a lock? I have a vector of objects, and a thread pool, and ...
0
votes
1answer
84 views

What OOP design pattern would work best for an overseeing class connecting related objects enforced at compile-time?

The Problem I would like to create a managing "overseer" class that connects several related object groups together where any particular group is able to be easily swapped for another using C++11 ...
0
votes
1answer
104 views

Re-usability in C++ using Interfaces and External Configuration

I am practicing professional C++ by building a differential drive robot that applies a Go-To-Goal Behavior. What is I have written so far is an Agent that consumes the following interfaces: ...
0
votes
1answer
84 views

Multithreading - 2 active running threads calling each other without blocking itself

I have an implemented UDP receiver which is continuously receiving DTN packets (UDP encapsulated) and performing some operations on them. This whole process is working on a single main thread. However ...
0
votes
1answer
114 views

Class holding collection of other class

I have a class Item. Then another class ItemCollection holds std::map<int,Item> item_collection_. The reason to create the class ItemCollection is if I want to do something with collection of ...
1
vote
3answers
318 views

What is late binding?

I know, there are lots of sources on the internet, but I do not understand them. Wikipedia: "Late binding, dynamic binding, or dynamic linkage is a computer programming mechanism in which the method ...
0
votes
0answers
91 views

Base class with bool flag to imitate virtual constructor

I am using bool flag to call base class function when needed to imitate virtual constructor. I am using this way for inheritance purpose. I have a Base class constructor with int and bool. The bool ...
0
votes
1answer
72 views

Avoid numerous dynamic_cast_ptr in the API design of polymorphic types

I am trying to design a geometric intersection API. Below is the code to represent geometric elements. #include <iostream> #include <memory> // Since I did not write the Shape class I ...
1
vote
0answers
39 views

How to specify constant data wrt cache coherence?

I am suspecting cache latency issues due to constant data (or same data across multiprocessor caches) being churned up in the cache coherence protocols. Is there a way to specify specific data as ...
0
votes
1answer
158 views

C++11 freeRTOS questions about queue usage and object design

Background I'm writing C++ firmware with freertos to penplottingrobot. It's a makeblock XYplotter and the firmware is connecting to mDraw software for command instructions thru USB drivers. MDraw ...
3
votes
2answers
1k views

Is the meaning of `const` still thread-safe in C++11?

I recently came across Herb Sutter's video from about how the meaning of const and mutable has changed in C++11 to mean bitwise const (and thread-safe, as a consequence) instead of the traditional ...
-1
votes
1answer
604 views

Combining the arguments of a function using a single structure

What are the consequences (pros and cons) of using a single structure as the argument, instead of a list of parameters. Some of the advantages I see are: create multiple default parameters. Need not ...
4
votes
2answers
787 views

Implementing reference counting from scratch or using shared_ptr for resource?

In an OpenGL application that I am writing, I want to have a simple shader class to wrap the OpenGL shader handle. Ultimately, I want this shader class to behave very similarly to a shared_ptr in c++ (...
17
votes
5answers
12k views

Is passing arguments as const references premature optimization?

"Premature optimization is the root of all evil" I think this we can all agree upon. And I try very hard to avoid doing that. But recently I have been wondering about the practice of passing ...
0
votes
1answer
398 views

Advantage or disadvantage of using = operator or uniform initializer while using auto keyword

Most examples of auto use the = operator. #include <iostream> int main() { auto a = 1; std::cout << a << '\n'; } Why don't they use the uniform initializer like this? #...
3
votes
2answers
2k views

'Assigning' a base class to a derived class?

I'm looking through some legacy code and I've come across a case where the developer has 'extended' an existing class with additional members, using inheritance as his weapon of choice. Essentially it ...
1
vote
1answer
113 views

Should repeatedly-computed quantities derived from member data be stored in member data containers?

This question is generally regarding when it is a good idea to store quantities derived from member data in, say, std::vectors that will be repeatedly computed/accessed in various methods, but I will ...
7
votes
4answers
6k views

Using vectors of shared pointers to objects in my C++ code to prevent object duplication

In my C++ project, I have three classes, Particle, Contact, and Network. The Network class will have N particles (std::vector<Particle> particles) and Nc contacts (std::vector<Contact> ...
6
votes
1answer
456 views

“The C++ Programming Language” confusion on Comparison

I am not grasping the idea from this quote by Stroustrup on page 862 of "The C++ Programming Language" 4th edition at the end of the first paragraph (I've highlighted the relevant portion): ...
1
vote
2answers
6k views

How to pass a mock as std::unique_ptr to Class under test

I am writing some test units using googletest and googlemock and I am stuck in a problem related to C++11 smart pointers and polymorphism. Suppose you have those classes: class A { public: ...
6
votes
2answers
335 views

What Design to choose for Parsing different files to populate different classes?

I am currently working on project which requires parsing different types of files. The content of the files will populate the classes. eg: file1 populate content of class1, file2 populate content ...
2
votes
1answer
77 views

Does an explicit temporary of an integral type qualify as an integral constant expression?

In the following code, int() is an explicit type conversion. #include <iostream> int main() { void* ptr = int(); return 0; } Clang Compiler: source_file.cpp:5:11: error: cannot ...
2
votes
4answers
1k views

Refactoring large code base legacy C++ project that mixed with C style

Background: Currently I am working on a C++ project. It's for business use, so this project is not open sourced. However, this is a project with a fairly large code base (around 10k lines of code) ...
1
vote
0answers
190 views

Constructing a stateful allocator using an interface

I have the following abstract class which implements the "Allocator" concept, using policies and traits to customize behavior: #define FORWARD_ALLOCATOR_TRAITS(C) \ typedef ...
0
votes
1answer
132 views

Function returning different tuples [closed]

I am trying to learn C++11/14 new features and I am playing with tuple and variadic. I wonder if it is possible to use tuples to return multiple values that are not fixed at compile time. Something ...
2
votes
5answers
892 views

Handling errors for non-exceptional cases in modern C++

To hone my skills - and for the pleasure of it - I am writing a small game for my kids in modern C++ (C++11, C++14 and the part of C++17 already supported by Visual Studio), which is a nice break from ...
0
votes
1answer
118 views

System design for a client server problem

I am trying to design and implement a solution for the following problem I am facing in one of my projects. There are n (say 30) clients that send me data points of the form {timestamp, object}, ...
3
votes
1answer
1k views

List initialization by empty braced-init-list (value-initialization or by initializer_list) vs explicit default initialization, for “list” types

Background Based on Item 4 in Scott Mayers Effective C++, when appropriate, it can be good practice to initialize all member properties of custom types via the member initialization list (in same ...
3
votes
1answer
161 views

What should I use for technically always read-only objects?

I have a function get() which gets a human object from somewhere. That is literally the only use of human, to provide a collection of properties of a human. struct human { std::string first_name; ...
7
votes
3answers
12k views

Is there any benefit to to define constant local variables as static ( c++)?

void Animation::playAnimation() const { static const int index = 0; const std::string& animationFileName = m_animationContainer.getAnimationName(index); static const int zOrder = -...
-4
votes
2answers
918 views

recommend a design pattern for a C++ application that takes some data, processes it, and returns results

So I am new to design patterns and C++11, but I have been studying both quite a bit. I am working on developing a C++ application to process some data, and I was hoping that someone with more ...
25
votes
2answers
6k views

Is it ever bad to mark a C++ function constexpr?

Given a very trivial function, int transform(int val) { return (val + 7) / 8; } It should be very obvious that it's easy to turn this function into a constexpr function, allowing me to use it ...
1
vote
1answer
68 views

Handling more specialized interface with no code duplication

I have the following code: struct AInterface { XXX some_method(/* some params */) = 0; }; void foo(const AInterface & a) { do_work(); while (x) { do_work_2(); ...
8
votes
1answer
357 views

Do C++'s iterator categories forbid writing a UTF-8 iterator adapter?

I've been working on a UTF-8 iterator adapter. By which, I mean an adapter that turns an iterator to a char or unsigned char sequence into an iterator to a char32_t sequence. My work here was inspired ...
4
votes
1answer
6k views

Why is accessing virtual protected functions of a base class not allowed through a pointer of the base class type inside a derived class

Consider the following: #include <iostream> class Base { public: void go(){this->doSomething();} protected: virtual void doSomething(){std::cout << "base";} }...
4
votes
1answer
154 views

Appropriate use of mediator pattern

I'm writing a program that has one class called Driver, which reads audio, sends it through an effects chain and then sends it to an audio output. The effects are created from dynamic libraries (using ...
9
votes
2answers
1k views

Is this a good approach for a “pImpl”-based class hierarchy in C++?

I have a class hierarchy for which I would like to separate the interface from the implementation. My solution is to have two hierarchies: a handle class hierarchy for the interface and a non-public ...
8
votes
3answers
4k views

Are all the objects in C++ mutable if not stated otherwise?

Is every object in C++ mutable if not stated otherwise? In Python and Javascript I can't change strings, tuple, unicodes. I was wondering if there is something in C++ that is immutable or every ...
1
vote
3answers
253 views

How to represent personality of a person in class?

Lets say I have a class Person. class Person{ Person(std::string, int); void walk(); void talk(); void eat(); private: int age; std::string() name; }; Now lets say I want ...
8
votes
1answer
701 views

Design considerations for configuration menu on embedded system

I am working on an embedded system that interfaces with the user with several buttons and a small graphic display. As a side note: Since I am on an embedded system, I would like to prevent dynamic ...
0
votes
1answer
2k views

Executing a periodic Task with std::async

I am writing a class which periodically has to check, if the data it manages is too old, if so it should delete it. I am new to the C++11 multithreading library and I have the following questions: ...
3
votes
1answer
258 views

When should a general purpose hash table assume hash equality implies logical equality?

For a general purpose hash table that aims for both high performance and correctness, when, if ever, does it make sense to assume that hash equality implies logical equality? To lay some ground rules ...
6
votes
5answers
393 views

Favored syntax for calling a function with parameters that another function returns

I'm about one week into studying C++ so please bear with me. So let us say that I have a function that returns a value that I need to use in another function. Is it better practice for one to assign ...
1
vote
1answer
710 views

Mocking of non-copyable objects

I find myself often in the situation where I want to mock a non-copyable object, for example a DbHandle handle. I was going back and forth looking at different design choices, and I settled on the ...
2
votes
2answers
231 views

Design classes to prevent future code breaks

I'd like to design a robust version of this class (C++11): class Oscillator { private: std::vector<double> trajectory_; // Some numbers that are needed to get the trajectory ...
5
votes
3answers
3k views

How to properly deal with starvation

I'm trying to find a way to avoid starvation in my program, a producer/consumer (two threads, one for each role) problem with four priority levels (four deques). Basically, the consumer thread always ...
1
vote
0answers
118 views

Managing reference-like members with shared pointers [closed]

It is a well-known fact that there is no built-in mechanism that prevents member fields that are references from being invalidated, even if they are const. (For more background see: https://herbsutter....
14
votes
1answer
597 views

possible to achieve Rust's ownership model with a generic C++ wrapper?

Looking through this article on Rust's concurrency safety: http://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html I was wondering how many of these ideas can be achieved in C++11 (or newer)....
4
votes
2answers
4k views

How to write unit tests for network client?

I need to write simple http client. It could be great to have unit tests for my class. But I don't know how to write proper and testable class. For example, I have a client like this: class ...