Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
0answers
79 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
61 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
35 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
134 views

freeRTOS C++11 queue with pointers inside, object-oriented

I thought about this problem I was having. I have producer-consumer type of design with two user-created-tasks sender, and reader. Queue is used to communicate between reader-sender. Currently the ...
0
votes
1answer
106 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
480 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
239 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
530 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++ (...
10
votes
5answers
6k 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
269 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
1k 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
108 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
4k 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
452 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
4k 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: ...
7
votes
2answers
301 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
76 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
880 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
149 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
120 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
754 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
107 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
916 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
160 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; ...
5
votes
3answers
7k 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
746 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 ...
-1
votes
0answers
116 views

“Canonical” set of operator overloads for a wrapper class

I'm working with a straight-C graphics API (Allegro 5) that defines a union type to hold data generated by various system events, call it "ALLEGRO_EVENT". The C API passes unions of this type to ...
22
votes
2answers
4k 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
63 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
322 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 ...
3
votes
1answer
4k 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
139 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 ...
7
votes
3answers
3k 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
243 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 ...
7
votes
1answer
627 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
233 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
383 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
604 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
230 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 ...
6
votes
3answers
2k 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
107 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....
13
votes
1answer
429 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
3k 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 ...
4
votes
1answer
1k views

Unique pointer initialisation

What is the correct initialisation of a smart pointer? std::unique_ptr<Class> ptr(std::make_unique<Class>()); or std::unique_ptr<Class> ptr = std::make_unique<Class>(); Is ...
1
vote
1answer
870 views

Async I/O operations - proper way to avoid deleting object

Using async I/O operations from boost::asio I often need shared pointers (and enable_shared_from_this and shared_from_this in callbacks) to avoid deleting objects too early. I think that it could be ...
5
votes
2answers
4k views

Designing an in-memory table in C++

I'm evaluating my options to structure an in-memory database and I have a few ideas of how to implement it. I would like to know your opinion of what the best design choice is. I have a column class ...
0
votes
1answer
145 views

Blocking queue for token based synchronization

I'm currently looking for a good solution for the following use-case in C++ (preferably C++11 or C++14): The program is running a server with a TCP connection. Other clients can connect at any time ...
12
votes
1answer
2k views

Object lifetime invariants vs. move semantics

When I learned C++ long ago, it was strongly emphasized to me that part of the point of C++ is that just like loops have "loop-invariants", classes also have invariants associated to the lifetime of ...