Questions tagged [c++11]

25650 questions
1

votes
1

answer
311

Views

GNU version issue with gnu++0x standard

I need to compile code with two compiler versions: g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3 g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 I have a piece of code in a header file like this: template class Constant { ... /*constexpr*/ static const RealType Pi = 3.141592653589793238462643383279502884197169...
Michael
1

votes
1

answer
746

Views

initializing unique_ptr at runtime

In my implementation, I have a vector of classes. Within each class there is a unique_ptr to a linked list. Only at runtime do I know the number of nodes that should be added to each of the linked lists. Some linked lists may have zero nodes. A simplified view of my class is: class A { private: ......
Gautam
6

votes
2

answer
87

Views

How to write a maintainable, fast, compile-time bit-mask in C++?

I have some code that is morally like this: #include enum Flags { A = 1, B = 2, C = 3, D = 5, E = 8, F = 13, G = 21, H, I, J, K, L, M, N, O }; void apply_known_mask(std::bitset &bits) { const Flags important_bits[] = { B, D, E, H, K, M, L, O }; std::remove_reference::type mask{}; for (const auto& b...
Alex Reinking
1

votes
2

answer
1.3k

Views

Storing std::promise objects in a std::pair

I'm working on a couple of C++11 work queue classes. The first class, command_queue is a multi producer single consumer work queue. Multiple threads can post commands, and a single thread calls "wait()" and "pop_back()" in a loop to process those commands. The second class, Actor uses command_queue...
dicroce
1

votes
2

answer
28

Views

initializing of sisterclass with inherited constructor

I have a base class which is inherited by 2 child classes. This child classes also inherit the constructor's. But if I try to construct instance of class A with a copy of class B it won't work (compile error). class base { int a_; public: base(int x) : a_(x) {} base(base const& cpy) : a_(cpy.a_) {}...
user1810087
9

votes
3

answer
501

Views

How can a variadic template be used to generate a left-associative expression (aka left fold) in c++11?

I would like to use a c++ template to aggregate (fold) multiple arguments using a binary operation. Such a template could be used as follows: fold(100,10,5) expands to add(add(100, 10), 5) The particular expansion shown above is the "left fold". The expansion add(100, add(10, 5)) is the "right fold...
ricovox
1

votes
2

answer
1.6k

Views

c++11 regex : check if a set of characters exist in a string

If for example, I have the string: "asdf{ asdf }", I want to check if the string contains any character in the set []{}(). How would I go about doing this? I'm looking for a general solution that checks if the string has the characters in the set, so that I can continue to add lookup characters in t...
dk123
1

votes
3

answer
444

Views

Loss of data while building a std::string from const char * or LPCSTR

I have a Function which returns a LPSTR/const char * and I need to convert it to a std::string. This is how I am doing it. std::string szStr(foo(1)); It works just fine in all the cases just when foo returns a 32 characters long string it fails. With this approach I get "". So I thought it had to do...
Genocide_Hoax
1

votes
1

answer
3.5k

Views

Does elegant way exist to convert double to long long?

I want to get integer part of double. But this way does not work for me: double param, fractpart, intpart; param = 3.14159265; fractpart = modf (param , &intpart); printf ("%f = %f + %f \n", param, intpart, fractpart); It is due to the fact, that double integer may have e-notation. For example, 3.41...
Denis
1

votes
2

answer
1k

Views

How to implement factory+decorator pattern in c++11

I decided to study/translate Head First Design Patterns' Java code to C++11, and I was able to implement most of the patterns using automatic memory management thanks to smart pointers. However, I have a problem with one of the examples. Here is my code: #include #include class AbstractBase { publ...
mty
1

votes
2

answer
97

Views

x86 relaxed ordering performance?

Since intel provides strong hardware memory model, is there any advantage at all to using "memory_order_relaxed" in a C++11 program? Or just leave it at default "sequential consistent" since it makes no difference?
excalibur
0

votes
0

answer
33

Views

Is this a valid use of strands?

I'm trying to write an abstraction layer on top of asio and asio::thread_pool. I currently have two methods, read(..) and write(..) that use strands and async methods to send and receive data from a socket backed by an asio::thread_pool: template auto abstract_connection::write(std::vector packet,...
ruipacheco
0

votes
2

answer
44

Views

How to ensure that initializer_list is not zero

I have a constructor that accepts a initializer_list as argument: A::A(std::initializer_list arg) : vec(arg) { } The issue is that it allows initializer_list zero or empty values for arg: A {} How can I force a non-zero initializer_list ?
Programmer
-1

votes
1

answer
26

Views

ThreadPool pass function as argument

I'm trying to implement my own version of parallel for using https://github.com/Fdhvdu/ThreadPool as the backend thread pool I'm splitting up the task to several slices and launching a thread with the following function: template void launchRange(int id, Callable func, int k1, int k2) { for (int k...
La bla bla
1

votes
1

answer
588

Views

Passing 2d std::array to function cpp

I am trying to write a function in c++ that will take 2 input std::arrays, and return an array of the products via matrix multiplication. However, the function cannot take an array with different dimensions (ex. 4x4 works, 3x4 does not) here is the code: #include #include template void dot(std:...
John Doe
1

votes
1

answer
2.2k

Views

Initialization of static const member map<> in class definition

I'm gonna to have a const std::map in my class, and I want it be static to reuse this data structure in other instance. Unfortunately, it won't compile and what I found in cpp primer is: However, we can provide in-class initializers for static members that have const integral type and must do so for...
Frank Wang
0

votes
1

answer
26

Views

What's the difference between the following ways of adding elements to a c++ vector [duplicate]

This question already has an answer here: std::vector::resize() vs. std::vector::reserve() 6 answers Why doesn't this vector assignment work? 1 answer Why aren't these vectors equal? 1 answer Snippet1: The following snippet prints out 0 1 but returns an empty vector. vector trial() { vector ret;...
user3828311
1

votes
1

answer
440

Views

How to pass custom allocator to std::basic_ostringstream in C++11?

I want to use a custom allocator to allocate memory from a freelist for std::basic_ostringstream. Here is my custom allocator which I want to use: template struct NAlloc { typedef Tp value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typ...
user3059007
1

votes
1

answer
29

Views

Segmentation fault while passing 2d vector of chars in to a function

I want to pass a variable which is of type vector p into another function declared as void foo(vector &var). To pass p into foo I simply did foo(p). While doing so I get the following error error: no match for call to ‘(std::vector) (std::vector&)’ I have followed the discussion given in here! U...
Geek
4

votes
3

answer
144

Views

Specialize member functions based on size of member container

I have a class that holds some statically-sized containers: template struct Point { Container container; ... void bar(); } where a Container class might look like: struct Container1 { static constexpr size_t size = 5; } Now I want to specialize the bar method based on the size of the container. I...
bremen_matt
5

votes
1

answer
200

Views

Heap allocation for std::array

According to this question std::array is allocated on the stack. However when using it together with Valgrind it shows me a heap allocation, even for elements which are allocated on the stack. Is this a false positive or real? Here follow two mwe to illustrate the behavior. No heap: The following co...
magu_
1

votes
3

answer
440

Views

Order of evaluation of assignment subexpressions

The C++11 standard (5.17, expr.ass) states that In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. With respect to an indeterminately-sequenced function call, the operation of a compoun...
Dariusz
3

votes
1

answer
47

Views

Why does stringstream stop receiving strings? [C++]

I'm trying to implement a way of reading inputs from text files to load different sets of coordinates more easily, but I've come across a bug I don't understand, where my stringstream object will stop receiving strings once one of the lines is ill-formatted. In my output, you can see that the stri...
pgavriel
-1

votes
1

answer
32

Views

Why is operator<< overload changing values of my variable?

I am expanding on a interview question I had. I created a Rectangle class and instantiated a few of them. I encountered a problem when the deconstructors are called. r1 was fine. r2 caused a crash. So I manually called the deconstructors and noticed it was crashing on r2. I thought maybe it was link...
Codarth
0

votes
0

answer
11

Views

Knuth Permutation Vector

I've been coding on and off for 3 months now and tend to draft out my code by hand before I implement it, so please go easy on me. ;-) here's a link for reference. The function in question is ‘solutions’ The print function is just an iterator I use to print vectors for testing. My full project...
user11069839
1

votes
3

answer
1k

Views

What design to apply for an embedded state machine [closed]

I need to make a state machine for a hardware device. It will have more than 25 states and I am not sure what design to apply. Because I am using C++11 I thought of using OOP and implementing it using the State Pattern, but I don't think it is appropriate for the embedded system. Should it be mo...
Kobe
0

votes
0

answer
61

Views

Should i stick with c++11? [on hold]

I have been sticking with C++ 11 standard for a long time (when learning c++), due to the fear of new standards may not be well supported on some platforms. When I was exploring on the Internet, trying to find some more handy libraries, most of those promising ones (at least in my opinion they are)...
Samuel Cheung
0

votes
1

answer
20

Views

Template keyword as template argument

Reading functional programming in c++ by Ivan Čukić seeing this towards the end of section 9.1.4. What is this syntax "template Variant" and "template Expected" coming from? template Expected get_if(const Variant& variant) { T* ptr = std::get_if(variant); if (ptr) { return Expected::success(*ptr)...
Kobi
1

votes
2

answer
147

Views

What is the actual result type of std::chrono::duration::count function

What is the actual result type of std::chrono::duration::count function like in the following case: std::chrono::duration_cast(end - begin).count(); It seems like it is some rep type but what is it actually? I need to know the exact type to pass it to some other languages. Can I just cast it to long...
FrozenHeart
1

votes
2

answer
100

Views

Calling `delete` on an object owned by a `unique_ptr` using another pointer

I have a pointer to class initialized by the new operator. Then I use this pointer to setup a std::unique_ptr. Now, as far as my understanding goes, the following code has double delete, once the manually called delete operator and then when the unique pointer goes out of scope. How does this code r...
mahesh
1

votes
2

answer
90

Views

How to make it work, problem with 2 classes

i have problem with my small project. I have two classes in it. Problem: error: 'Display' was not declared in this scope Display is a class. Here is code: //main.cpp #include #include "Display.h" #include "Polynomial.h" using namespace std; int main() { Polynomial prr; prr.show(); cout
Blue shirt
1

votes
1

answer
51

Views

Where to put non-member function definition of a PRIVATE nested (inner)class?

Following is a simple example of my actual problem. I have a non-member function defined using friend keyword in the InnerClass. The definition of this function should be outside of InnerClass. On the other hand, the class definition is under private of MyClass as I want to use the InnerClass specif...
Const
1

votes
2

answer
54

Views

Get wrong date and time with high_resolution_clock in Visual Studio 2017 / Get number of milliseconds from epoch time (1 jan 1970)

I have a problem with Visual Studio 2017. I'm trying to get the current time and date with millisecond resolution. I tried the follwing code in a few compilers: #define _CRT_SECURE_NO_WARNINGS #include #include #include using namespace std; using namespace chrono; int main() { high_resolution_clo...
nano
1

votes
1

answer
25

Views

c++11 initializer_list doesn't work for literal constant value of embedded object?

I've got a simple program in c++11: struct A{ int i; struct B{ int i; int j; }; } a = {2, {3, 4}}; g++-7 compiles and gives error: error: too many initializers for 'A' }a={2,{3,4}}; ^ I just wonder how can I declare an object of A using literal constants, how to fix it? Thanks a lot.
Hind Forsum
1

votes
1

answer
82

Views

Implementing 64 bit atomic counter with 32 bit atomics

I would like to cobble together a uint64 atomic counter from atomic uint32s. The counter has a single writer and multiple readers. The writer is a signal handler so it must not block. My idea is to use a generation count with the low bit as a read lock. The reader retries until the generation count...
ridiculous_fish
23

votes
5

answer
1.9k

Views

Why are literals and temporary variables not lvalues?

I've read that lvalues are "things with a defined storage location". And also that literals and temporaries variables are not lvalues, but no reason is given for this statement. Is it because literals and temporary variables do not have defined storage location? If yes, then where do they reside if...
pasha
0

votes
0

answer
8

Views

no matching function for call to 'make_shared'

I am getting compiler error "no matching function for call to 'make_shared'" whenever I try to use a constructor that takes any arguments. So, for example: std::shared_ptr foo = std::make_shared (); works fine. But, std::shared_ptr foo = std::make_shared (10); Gives the following error: /usr/bin/c...
Daniel Goldfarb
1

votes
1

answer
67

Views

std::map Why it is not adding key , values in my code

I am trying to add key & value data to my class map member variable - but it does not adds the same -I tried map - insert, [] and emplace method but they are not adding data to map in my loop code - only the value I have inserted during my class construction is available in it - what I need to do to...
Programmer
-2

votes
0

answer
41

Views

How do I pass all vector elements separated by comma into function in C++?

I have been reading this website for quite a while now but have just registered. I have also used search which did not seem to be very helpful. Here it is: As I am having fun with C++ I come along "lazy evaluation" conception on the Internet. I am interested in creating something like a "lazy vector...
Nick M
1

votes
0

answer
10

Views

Gtest: mocking free function in a constructor

I read a lot of documentation related to Gtest mocking (e.g., https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md, "Mocking Free Functions"), but couldn't find the solution for the following problem: source.cpp H::H() { // some code1 if (to_be_mocked(id) != 0) { // some code...
David Hovsepyan

View additional questions