One of the stated advantages of initializer list syntax is that it improves type safety by prohibiting narrowing conversions: int x {7.9}; // error: narrowing int y = 7.9; // OK: y becomes 7. Hope for a compiler warning However, AFAIK there is no way
I was reading an article about Overloading the Stream Insertion Operator (<<). It stresses that the output stream object ought to be returned to ensure the operator is cascaded correctly. But it seems without the return, the output is still correct,
Recently I have been working on remaking my remake of the classic snake game, this time employing a deque instead of a linked list for the snake's tail. I construct a tail segment and try to emplace it at the front of the deque and get a strange erro
So, I have an application where if a particular button is kept pressed it plays an audio device, when the button is released it stops the audio device. I use keyPressEvent and KeyReleaseEvent to implement this which is similar to the code below: void
I want to replace all occurrence of 'a' with 'b', and 'c' with 'd'. My current solution is: std::replace(str.begin(), str.end(), 'a', 'b'); std::replace(str.begin(), str.end(), 'c', 'd'); Is it possible do it in single function using the std?Tricky s
Do I need to wrap std::unique_ptr usage with try/catch in code which should be exception safe? std::unique_ptr will hold a raw memory block created by ::malloc (with my custom deleter to call ::free).All of std::unique_ptr's constructors* are noexcep
I'm trying to make a function that accepts a shared pointer to some functor. With manually crafted functors there're no problems, but with lambda there are. I know that I can't use decltype with lambda - every new lambda declaration creates a new typ
I have a method that takes a parameter which is a reference to a base class and I enqueue invocations of the method body by wrapping the method implementation in a queue<function<void()>> The issue is that I was hoping to capture the method's
Trying to save one iteration over my vector. I have a vector<T> I need to find a minimum element in the vector. I am using std::min_element. Now there is a requirement to create a custom structure, which has elements of <T> plus few extra, com
The problem is clock() function is not allowed, but I have no idea how to deal with time() function in thread.I think you can use gettimeofday() function to get the start time and end time. While this function work only in Linux. Please refer to [1]:
Writing library-like code in C++ I found there is particular need in copy_cv_reference_t type trait: struct A; struct B; static_assert(std::is_same< copy_cv_reference_t< A , B >, B >{}); static_assert(std::is_same< copy_cv_reference_t< A
What does the C++11 standard specify for the behavior of the string& erase (size_t pos = 0, size_t len = npos); member function when the pos argument is passed as string::npos? I would think it should erase nothing, but perhaps it throws an out_of_ra
I'm writing code with a lot of STL vectors in it. I think I've structured it so that it's all references and move constructors, but I'd like an automated way to be sure. Is there any way to get a warning or error whenever the copy constructor gets in
For educational purposes I want to write my own c++11 based typelist. The bare list looks like this: template <typename ... Ts> struct type_list; template <typename T, typename ... Ts> struct type_list<T, Ts ...> { typedef T Head; typede
I am trying to make a resource class for my game (which makes use of the SFML API). Basically I first load the needed resources and then I just get references to them when needed in order to avoid the heavy construction of the resource classes. (I wi
With a regular C++98 program, to run it from TextMate, all I need to do is a ⌘R to run it from TextMate. With C++11, however that does not work out of the box. For instance if I have the following program: #include <iostream> using namespace std; in
In these slides about C++11/14 standard, on slide 15, the author writes that "many classic coding rules [are] no longer applicable" in C++11. He proposes a list of three examples, and I agree with the Rule of Three and the memory management. How
Here is some example code: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const Foo& lhs, const Foo&
I am trying to understand rvalue reference and move semantics. In following code, when I pass 10 to Print function it calls rvalue reference overload, which is expected. But what exactly happens, where will that 10 get copied (or from where it referr
C++11 §3.8.1 declares that, for an object with a trivial destructor, I can end its lifespan by assigning to its storage. I am wondering if trivial destructors can prolong the object's lifespan and cause aliasing woes by "destroying an object" th
Erase from vector giving different results with lambda function and function object. I'm trying to delete 3rd element from a vector of strings. With function object 3rd and 6th element is getting deleted but with lambda version the code gives expecte
Taking into consideration the entire C++11 standard, is it possible for any conforming implementation to succeed the first assertion below but fail the latter? #include <cassert> int main(int, char**) { const int I = 5, J = 4, K = 3; const int N = I
I've made a class in my header file which creates and populates three arrays like so: class ExampleClass { private: string array1[5] = {"test1", "test2", "test3", "test4", "test5"}; double array2[4] = {20.
It appears that std::remove_const isn't able to remove the const-ness of const char*. Consider the following code: #include <iostream> #include <type_traits> #include <typeinfo> template< typename T > struct S { static void foo( )
What's the best way for setting an std::vector<int> to a range, e.g. all numbers between 3 and 16?You could use std::iota if you have C++11 support or are using the STL: std::vector<int> v(14); std::iota(v.begin(), v.end(), 3); or implement yo
I have a class which derives from enable_shared_from_this and a method which returns a shared pointer by calling shared_from_this(). I would like to in that method detect if the object is owned by a shared_ptr and if not throw. I tried something like
Let say we have a base class and its two derived classes; The base class owns a method execute and each derived class implements a different version of this method with different types and number of arguments; I can't use a virtual method because sig
Some time ago I was told, that the usual pattern to implement two-ary operators needs a final move in the return. Matrix operator+(const Matrix &a, Matrix &&b) { b += a; return std::move(b); } But now there is the special rule that in a return
(Note: tuple and tie can be taken from Boost or C++11.) When writing small structs with only two elements, I sometimes tend to choose a std::pair, as all important stuff is already done for that datatype, like operator< for strict-weak-ordering. The
I would like to pass a lambda to a funciton. This boost::function<void()> fncPtr(boost::bind<void>([](){/* something */})); works, but if the lambda had a parameter, I don't know how to do it properly: boost::function<void(bool)> fncPtr(