Type security during the assignment and initialization

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

The flow insertion operator cascade does not work

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,

QKeyEvent isAutoRepeat does not work?

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

Replace multiple pairs of characters in a string

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

std :: unique_ptr and exception security

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

Smart pointer to lambda

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

how to time the execution time of the included program

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]:

Type character to copy cv reference qualifiers

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

Can I disable the std :: vector copy constructor?

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

Struggling with the implementation of a list of types

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

Move semantics for a resource manager class

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

Build C ++ 11 with TextMate?

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

A virtual destructor with virtual members in C ++ 11

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

Do trivial destructors cause aliasing?

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

Vector erasure giving unexpected result

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

Remodeling a 1D array into a multidimensional array

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

How to remove the const from 'char const *'

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( )

Define std :: vector & lt; int & gt; to a range

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

Detect if an object belongs to a smart pointer

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

Pass lambda with the parameter

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(