Newest 'c++11' Questions

Q&A for professional and enthusiast programmers

Copy from std::deque to thrust device_vector

I'm trying to make a sample code where I copy from a std::deque to a thrust::device_vector but the compiler warns calling a __host__ function from a __host__ __device__ function is not allowed (I ...

Store shared pointer to array in vector

I’m trying to store a shared pointer to a fixed size array in to a vector, I want to use a shared pointer because I must pass a pointer to the array to another class that will write in the array, and ...

Tracking memory consumption of a template class

I have a class for which I'd like an estimation of how much memory it's consuming: template <typename T, typename U> class MyTemplateClass { size_t EstimateMemoryConsumption() const { ...

C++11: Function template returning 2D containers of arbitrary types

I'm trying to solve a college problem, that, among other things, requires us to write a function which takes a parameter: container of Type1, that contains containers of Type2, which contain ...

why the condition_variable waits indefinitely

Consider the following code snippet #include <future> std::mutex asyncMut_; std::atomic<bool> isAsyncOperAllowed = false; std::condition_variable cv; void asyncFunc() { while (...

Creating an Modbus RTU request c++

Hello all I was needing help with constructing a Modbus RTU packet request. Lets say I want the analog holding register(function code 3) starting from address 40108 to 40111 so that 4 registers and ...

How to set a function as a default parameter?

I ran into a problem with setting a function as a default parameter. The following code doesn't make a lot of sense. What I want to achieve can be done in many different ways. This code only ...

share instance of a class between many source file [duplicate]

i'm facing the following problem: i need to share a single instance of this class between many source files: class Cleaner{ static std::vector<GLuint*> vao_handler; public: static Cleaner&...

NFA to DFA implementation in C++

The title says it all. I need some ideas. The nfa input looks like this and has no epsilon moves. 1 2 0 a 2 1 b 3 etc, meaning that 1 and 2 ar final states and that with 'a' I can get from state 0 ...

C++ std::vector::push_back vs std::vector::emplace_back to a std::vector<std::string>

We have a similar question in here but I think we haven't got a clear answer the I remake the question. The C++11 standards has two ways to add a new element in the end of vector they are std::vector:...

how to insert numbers in a list c++ style using python [duplicate]

i have this c++ code. int range = 0; cin>>range; int input[range] = {0}; // inserting in array for(int i=0; i<range;i++) { cin ...

Class (C++) Overloaded Operators Causes Memory Leak

I'm developing C++ library based on GMP to work with bigintegers and bigdecimals. Class: class bigdecimal { public: mpf_t x; // and overloaded constructors methods... // I've provided an ...

Is this unsafe usage of a braced initializer list?

I had a bug crop up in my program recently that surprised me a bit, but perhaps it shouldn't, with the vast number of types of initialization that C++ (especially modern C++) provides. I have a class ...

Duplicate code using c++11

I'm currently working on a project and I have the following issue. I have a C++ method that I want to work in two different ways : void MyFunction() { foo(); bar(); foobar(); } void ...

C++ - Why does this ONLY statement make the code work? [on hold]

Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than ...

Nested lambda function

I have the following code: auto get_functor = [&](const bool check) { return [&](const foo& sr)->std::string { if(check){ return "some string"; } ...

welsh powell algorithm code giving segmentation fault

I've found an implementation of famous welsh Powell algorithm code in c++, and just to check it's working I compiled the code and executed it. unfortunately, it's giving me a segmentation fault and I'...

Clone pattern for std::shared_ptr in C++

Why do you need (in order to make it compile) the intermediate CloneImplementation and std::static_pointer_cast (see Section 3 below) to use the Clone pattern for std::shared_ptr instead of something ...

Runtime overhead of shared_ptr for subclass destruction compared to virtual destructor

I came across the shared_ptr trick for mimicking the behaviour of virtual destructors on a Youtube video (https://www.youtube.com/watch?v=ZiNGWHg5Z-o&list=PLE28375D4AC946CC3&index=6), and upon ...

Compare two multisets of types for equality

As this question doesn't seem to cover all useful cases I decided to fill the gap with this little question of mine. Is there a way to answer if two multisets of types are equal? #include <tuple&...

C++11: should I use valarray or vector for numerical computing

The question of vector vs valarray has already been asked here. My question refers specifically to the case of C++11. I have been reading a "A Tour of C++" and "The C++ Programming Language". Both ...

Dangling reference in inner lambda [duplicate]

I have an inner lambda that uses one of the referenced variables of the outer lambda like this: int x=0; auto outer=[&](){ return [&](){ x=5; }; }; auto inner= outer(); inner()...

How is std::is_integral implemented?

I am not familiar with Template magic in cpp. After reading what 'TemplateRex' said in this link, I was confused about how std::is_intergral works. template< class T > struct is_integral { ...

Getting lots of error messages like “undefined reference to…” using FLTK with g++

I learn c++ following The Stroustroup's book (Programming: Principles and Practice), so I've been trying to run an example using FLTK libraries (I use Ubuntu 14.04). Actually, when I compiled the ...

getting segmentation fault in C++ code [on hold]

I am new to c++, working on my company's assignment my c++ code is giving me segmentation fault error, please help me in this. FetchingAndSortingFiles.h class FetchingAndSortingFiles{ ...

Threadpool C++ 11 game engine

It's time to implement a threadpool in my game engine with std::mutex, std::condition_variable, std::unique_lock and std::lock_guard. I have read a lot about Lock-free Programming, but that seems ...

How to decompose a monotone polygon into two monotone chains

I'm trying to triangulate a monotone polygone. The algorithm requires to represent the polygone like 2 monotone chains. There is no description, how to do it. Is there any simple algorithm, which ...

How to sort the segments c++

I have the vector of Edges. I need to sort those edges by y-coordinate of the point of crossing vertical line x==(a+b)/2 and those edges. The problem is the that a and b are not constant and they have ...

Non-copying std::shared_ptr<boost::any>?

I store "instances of different types" with "shared ownership". That's what I currently do: class Destructible { public: virtual ~Destructible() = default; }; // UGLY class MyType1 : public ...

How do I clear a screen efficiently

Whenever I try to make a game or something that requires the rapid clearing of a screen, I've used system("CLS"). I now know that that is not good to use so I tried using a for loop to print new lines ...

Polymorphism doesn't work in C++ lamda function.

I have a method like this: void syncOperation(ProgressCallback& progressCallback); where ProgressCallback is: class ProgressCallback { public: virtual void onProgress(std::size_t currValue, ...

C++ find substring with character mutation?

say, I have a string abcdefkkkkk and I have a pattern defg ,and want to match defk from the above string ,which means allow a different character. so I want to get defk from the string(or the ...

std::istream_iterator<double> member variable fails while incrementing

I have a program as follows: #include <iostream> #include <iterator> #include <fstream> #include <string> class Stream{ public: //default constructor ...

VB Function InStr() Equivalent in C++

I'm converting VB to C++. I don't know how to convert InStr() to C++. I have manged Left() but InStr() is not available in C++. Can Someone please tell me how to convert InStr() to C++. Public ...

Clean cancellation of an std::thread that is blocked on an input/output call

I have an std::thread that might be blocked on a file descriptor input/output call, how can I cleanly cancel it? Consider the following sample: #include <unistd.h> #include <thread> ...

Best way to implement functions with generic, mutable MatrixBase<Derived> references to work with Blocks in C++1y? (working example included)

Background First started tinkering with extending initializer lists to use submatrices, and wanted to know how to generalize methods to work on mutable matrices (e.g., Matirix<...>, Block<......

How to declare a vector in a hpp?

Hello I know that to declare a std::vector I have to do like this std::vector<double> a(0); But in my file it does not work. Here is my code : main.cpp : #include "test.hpp" int main() { ...

C++ explanation. [on hold]

int prod(1);   for (int m=1 ;m <= 9; m++){   prod *= m;   m += 2;   } // this equals 28 // Can somebody explain the steps C++ takes to get 28

How to pass in parameters in C++?

I'm debugging this function that was declared with three params, but actually passed in two, is it allowed or maybe it didn't call it directly? ConfigList filter( const BidRequest& br, ...

Conditional initialization of a const variable

The following base code is part of a quite large procedure: int x = foo(); if (x == 0) x = bar(); x is not modified anywhere else, so I'd can do: const int x = foo() == 0 ? bar() : foo(); But foo()...

How to insert elements into a multidimensional vector of unique pointers?

I have a Turn class with a _rounds private member. _rounds is a bidimensional std vector of std unique pointers to another class called Animation: Turn.h std::vector<std::vector<std::...

Which should I prefer for a constant within a function: constexpr const or enum?

I'm used to definition my constants with enum { my_const = 123; }, since in classes, using static constexpr requires some code outside of the class definition (see this question). But - what about in ...

Does declaring a variable inside a loop body have any disadvantages? [duplicate]

Suppose we have a loop that iterates many times: for (int i=0; i < 1000000; ++i) { int s = 100; s += i; cout << s; } We are only using s inside the loop body, so ideally we'd like to ...

How to return an unsimplified Expression

I am building a "simple" calculator that uses polymorphism and its recursive. I am a little bit stuck when trying to return an unsimplifiable expression. if I get as an input 3 / 2, how should i ...

unrestricted union members lifetime during parent object construction

Normally you are responsible for lifetime of your unrestricted union members -- and typically you do it via in-place ctor/dtor calls. But, apparently, there is at least one case when compiler helps ...

Multiply NS-3 Vector3D by a double c++ [on hold]

I have a position and direction vectors and a delta T double. I want to calculate the estimated position based on those values; however, I cannot multiply a vector by a double. Speed is constant at 1 ...

C++11 regular expressions and the string u8 prefix

Though I've tested it myself for some time now, I am still not sure whether the following has any effect. Say I have a regex of the form regex reg(R"(\s+)" + a + R"(\s+(.*?)\s+)" + b + R"(\s+)", ...

Does creating an object of a class in every iteration corrupts memory

I have two classes let's call them A and B class A { ....some function definitions.. specialFunc1(int count, B b) { stores data in the class B's object to its own containers ...

How to access the latest bit from a string?

I am doing some tcp/ip query response. So now I have extracted the bytes that contain the status of my sensor as a string. std::string loadingSeqStatus=data_buffer.substr(10, 1); Now every bit in ...

Fix compile error when template instantiated with non-scalar type

#include <limits> #include <iostream> using std::cerr; template< typename T > int get_max_if_integral() { if ( std::is_integral<T>::value ) return (int)...
Translating... 0%