c++ conditional generic class object

I have the following piece of code: helper.hpp : struct A { uint32_t a, b; }; struct B { uint32_t a, b; }; template <typename T> struct C { T barcode; }; Now based on some condition I want to create appropriate struct object in the...
more »

2017-07-01 21:07 (4) Answers

Sorting multiple vectors in single sort call C++

This answer demonstrates how to efficiently obtain an indices vector using std::sort on a vector of values using the nice new-ish C++11 functionality (there's also a variety of duplicates of that question as well). It also hints that you can obtain t...
more »

2017-06-30 23:06 (2) Answers

Combination of elements in an array returning sum

I am learning optimization problem using data structures. For that I have considered a problem in hand. I have an array like: movies[] = { 2,3,4,5,6,7,2,4,9 } I have a sum value: k = 5 Now I am finding the combination of array elements returning...
more »

2017-05-22 19:05 (1) Answers

Make a class that cannot be instantiated?

Suppose that I have a class Bar. I want to use Bar uniquely as a base class, e.g.: class Foo : public Bar I want to disallow Bar from being able to be instantiated. In other words, I want Bar object; to be an error. Is this possible to do? ...
more »

2017-05-15 00:05 (1) Answers

Passing a class member function to for_each

My question is: how do I pass a class member function into for_each Code I am trying to get to work: (works when function is defined outside of class) The part which fails is commented out - the one using for_each with function as the class member ...
more »

2017-05-09 23:05 (1) Answers

Is it thread safe to modify a static variable?

Since C++11, static variable initialization is guaranteed to be thread safe. But how about modifying a static variable in multiple threads? like below static int initialized = 0; Initialize() { if (initialized) return; initialized = 1; // Is t...
more »

2017-05-08 01:05 (5) Answers

Class with static linkage

If I recall correctly, static linkage means that a variable or function is local to its compilation unit. Meaning that there could be variables or functions with the same name and parameters in other compilation units. I want this for a class. Let'...
more »

2017-04-28 20:04 (1) Answers

Overloaded Functions and function type

I am reading C++ primer and my question comes from 14.8.3 Callable Objects and function. In the Overloaded Functions and function section, it says: We cannot (directly) store the name of an overloaded function in an object of type function: int add...
more »

2017-04-28 06:04 (0) Answers

std::sort by unary mapping

The C++ standard library offers to pass a Comparator to std::sort. However, I have many cases in my code where I want to sort a list of T objects by a function f. A comparator like this would be a valid option: bool compare(const T& a, const T&a...
more »

2017-04-16 15:04 (2) Answers