Newest 'c++11' Questions

Q&A for professional and enthusiast programmers

Regex: Ignore trailing whitespace in capturing group

I am writing a simple config file parser in C++11 and want to use a regex to pick out the key=value pairs from a text file. I want to be able to support comments in this file, denoted by a # symbol. ...

Operator overloading =, *=. How to make this work?

I have problem with those lines in main: *tab[1]=test1; *tab[4]=test2; It just adds colour, and variables (a,b,h) stay the same. I was trying something like this cuboid operator=(const cuboid & ...

What data structure should I use to model a database/table?

Databases usually look like this ┃Name|Age|..┃ ┠────┼───┼──┨ ┃John│025│..┃ ┃Carl│033│..┃ ┃....│...│..┃ In this case I mean a table with a fixed column size and a variable size of unsorted rows which ...

How to avoid dynamically allocating small objects?

I found that many times I only need a small std::map (say less than 10 keys), or a small std::vector containing only a few elements, and I think it's really a waste of performance to always ...

Using std::conditional_variable to wait on a condition

For simplicity, let's assume that we have only one conditional variable to match a single condition that is reflected by a boolean. 1) Why does std::condition_variable::wait(...) locks the mutex ...

how to create multidigraph with string using eulerian circuits?

If we have C/G, AU/G ,C/U*AU ,AC/U/AU/AC/G, G/G/AC, G/AU, G/C as the middle part and the beginning start with AU and ends with CU. How can we make a Eulerian circuits? The "/" are option that it can ...

How can I shuffle a vector with unique pointers in a random sequence with std::shuffle?

I am busy programming a card game. For this card game, I want to shuffle the deck. The deck is a vector of unique pointers and looks something like this: std::vector<std::unique_ptr<Cards>&...

bitwise AND with std::atomic<bool>

I have an error status boolean that is read/written by multiple threads. It is initially set to true, then should be set to false if any operation fails. I wanted to use a bitwise AND like so: std::...

C++11 redeclaration in range-based for loop

This code compiles in Visual Studio 2015 update 3 (and here: visual C++ compiler online , and does not in other compilers I have tried online (GCC and CLANG), giving a redeclaration error vector<...

How can I pass an initialized object

Currently, in my code I have vector_2 v1(1.0,2.0); Frame.Draw( v1(2.0f,1.0f) ); where vector_2 is a struct with a constructor that takes 2 doubles (coordinates). Frame is an instance of some class ...

Micro thread in c++

It is possible to reserve memory per thread in application with using std library? I want to create some micro threads and I would like to know how to reserve memory per thread and if it is possible ...

How to read binary data from fstream into std::array

It is often considered good practice to use std::array instead of a plain C++ array. What is the preferred way to read data from a file stream into in std::array. I have seen one topic where it as ...

Is this a bug in the boost library

I'm compiling a project with autotools and am using g++ in version gcc version 5.3.1 and boost version 1_63. After aclocal, autoconf, autoheader, automake --add-missing, I ran configure with some ...

why {} trigger Widget(0) instead of Widget(-1)? [on hold]

class Widget { public: explicit Widget(int = -1); ... }; std::array<Widget, 5> arr4; // construct all Widgets from -1 std::array<Widget, 5> arr5 {}; // construct all Widgets from 0 ...

Creation of duplicate files in form : “filename.cpp~” when compiling C++

<152 xserver2:~/SS17/Work >ls a.out helloworld2.cpp helloworld2.cpp~ helloworld.cpp <153 xserver2:~/SS17/Work >rm helloworld2.cpp~ rm: remove regular file 'helloworld2.cpp~'? yes <...

C++11 thread pool example with bug

Below is some code showing a simple and short implementation of a thread pool. The code is inspired by this post. I compile it with clang++ -std=c++11 threadpool.cpp -o threadpool -lpthread When ...

Using cv::Mat1f as cv::Mat

I have this method: static void WriteMatVect(const std::string& filename, const std::vector<cv::Mat>& mats); ... void FileSystem::WriteMatVect(const std::string& filename, const ...

The command “sudo apt-get update” always ocurred errors

The old g++/gcc version on my computer is 4.6.3. I need to update it to 4.8, because some softwares I want to install are required. But when I update g++/gcc on ubuntu 12.04 as follow command, there ...

Find the nth term of the recurrence relation, given the first 2 terms, f(0) and f(1) [on hold]

f(n) = f(n-1) + f(n-2) + f(n-1)*f(n-2); 1 <= n <= 10^9. Initial terms --> 1 <= f(0), f(1) <= 10^9. Output the answer modulo 10^9 + 7. The general term that I have found is, f(n) = (((1 ...

Auto type deduction and passing a lambda to an std::function parameter

Lets assume that I have a function with the following signature: void foo(std::function<void(int&)>& bar); The following call compiles successfully: foo([](int& x) { ++x; }); ...

Why doesn't the C++11 compiler complain when I initialize a local variable with the same name multiple times within the same scope?

So if I initialize a variable of the same type and name within the main function the compiler will complain of the re declaration, for example: for (int i = 0; i < 5; i++) { int a{5}; int ...

How to make a function generic for various, highly similar types?

I have a linked list format that is of various types (int, double, for instance): struct DblNode { double value; DblNode * next; } struct IntNode { int value; IntNode * next; } And ...

Compare std::set with custom comparer

I am trying to use the std::tie to implement the operator< in order to create a map of structs that contain a set. The same code without templates seem that it works. I am getting this message code ...

Why did I get the: “Attempting to reference a deleted function” error after adding a vector with unique pointers to my header file? [duplicate]

To learn C++ I am programming a game. In this game, there can be two players. Those players, are able to connect to each other with sockets. After adding a vector with unique pointers to the player ...

Varying number of definitions in a variadic template class

I recently thought of a problem. Let's say there is a variadic template like this one: template <typename... Types> class example { //something }; Now what I want to achieve is that when I ...

Dynamic function call with or without argument and its storage in a class

I use c++11 and it's not possible for me to use QTimer because my program is not graphical. I don't understand how to store my pointer function with their arguments and call this function in my run ...

segmentation fault when function called repeatedly [duplicate]

I am trying to run this code which for some reason crashes during execution with "Segmentation fault (core dumped)" error.When i ran this in Code::Blocks debugging mode,call stack showed me a ...

C++ use of deleted function std::unique_ptr with Base class

I have a vector or Game objects. I have to compile for c++11 std::vector< std::unique_ptr<Game> > games_; Game is the base class, defined like this class Game { public: Game(...

Dynamic allocation of class array with protected destructor

If I have a class defined like class A { protected: ~A(){ } }; then I can dynamically allocate the individual as well as array of objects like A* ptr1 = new A; A* ptr2 = new A[10]; However ...

C++ overloading operator with/without defining a template

Problem: overload the default << operator for a vector<int> v variable, so that cout << v will print each element sequentially. Here I saw one option as proposed by Jason Iverson in ...

Interfaces and public methods

I have a struct that I use a pure abstract interface (only public methods and they're all =0), an abstract class to hide implementation details and two specialisations that inherit from it. I'd like ...

Is it possible to store different class with template member function in a container

That's a hard one for me. I need to store different classes with same template member function in one container. The member function is always the same declaration. struct foo_creator { template &...

Node Type info from a namedDecl() matcher

I'm writing an open-source misra-c rule checker using the clang-frontend libraries. Is there a way to get the type of a node(FieldDecl,RecordDecl,FunctionDecl,...) that I've matched with a namedDecl() ...

Implicitly default-constructible meaning?

On this reference page for std::tuple it is said that the default constructor for the type is "explicit if and only if Ti is not implicitly default-constructible for at least one i". I'm a bit ...

Can you safely use a std::set as the key to a std::map? [duplicate]

I have defined the following: typedef std::set<std::string> entry; typedef std::map<entry, size_t> freq_map; In this way, I can read in lines from a text file of this form and keep track ...

Deduce the std::function result type and use it as a parameter

This is how my simple class looks like. template <class T> class A { T first; T second; public: A(T f, T s) : first(f), second(s) {}; template <class F> auto foo(F f) -> decltype(f(T(...

User defined literals definitions

I was taking a look at the cppreference page for user defined literals, and I think I understand everything except a few examples template <char...> double operator "" _π(); // OK How does ...

Find the indices and inverse mapping of a unique vector

I have a std::vector<int> with duplicate values. I can find the unique values using std::unique() and std::vector::erase(), but how can I efficiently find the vector of indices and construct the ...

Are bits significant in endianness?

I'm attempting to handle endianness in a cross-platform C++11 application. Suppose I'd like to convert a 64-bit value from big endian to little endian with well-defined behavior. One resource ...

C++ Conditional Templates Compilation based on data type

I was really hoping I could achieve this using C++ (11) Templates, but I'm facing some problem here. So we have a custom pointer object which internally could be of any of these types: a list of ...

Lifetime of temporaries in initializer_lists nested in function calls

What is the lifetime of a temporary in something like func({someString, std::string("foo")})? I ask because the initializer_list in question is actually of type initializer_list<Foo> (not ...

C++ std::regex_replace rgx and fmt arguments to add a leading zero to exponent in scientific notation?

Problem: With the VS 2015 compiler, sprintf prints only two characters for the exponent in scientific notation. I want to print three characters like the VS 2012 compiler does. My solution is to post-...

implementation of std::begin in gcc stl (why two overloads?) [duplicate]

In: C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\lib\gcc\x86_64-w64-mingw32\6.3.0\include\c++\bits\range_access.h there is following implementation of std::begin, there are ...

Different compiler behaviors on type trait to test implicit default-constructibility

I've been trying to write a type trait with C++11/14 that tells if a type T is implicitly default-constructible. Examples of implicitly/explicitly default-constructible types: struct Imp { Imp() = ...

How to use “using” for a function?

How to use "using" for a function? For example class A; void f(int); struct B { using BA = A; using Bf = f; ??? };

C++ - How can I delegate all member functions invocations on a class to a member of the same interface

I have an interface and a couple of classes that implement that interface. I also have a class which simply acts as a wrapper that simply delegates all calls to a member that also implements that ...

How to generate a non-repetitive sequence of random numbers using std::rand

Reading std::uniform_int_distribution in the final example the generated sequence is: 1 1 6 5 2 2 5 5 6 2 How can I guarantee that all the generated numbers are different between them? And if this ...

C++: Core dump with packaged_task

I got a strange core dump which I copied from a part of code in http://en.cppreference.com/w/cpp/thread/packaged_task, #include <future> #include <iostream> #include <cmath> void ...

Why standard library function of a constexpr is not a constant?

constexpr PixelType maxVal = std::numeric_limits<PixelType>::max(); constexpr double lnFactor = std::log(maxVal); Second line gives error C2131: expression did not evaluate to a constant Why? ...

Identify pointers in a tuple c++11

I need to convert a tuple to a byte array. This is the code I use to convert to byte array: template< typename T > std::array< byte, sizeof(T) > get_bytes( const T& multiKeys ) ...
Translating... 0%