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

Is move semantics in C++ something C is missing?

I have been searching for this matter on SO and other sources but I couldn't wrap my head around this issue. Using resouces of rvalues and xvalues somewhat new to C++ (with C++11). Now, do we - C programmers - miss something here? Or there is a cor...
more »

2017-04-07 07:04 (5) Answers

How use integer_sort from boost library?

I use boost::sort() from boost/range/algorithm.hpp but is really... slow and I'm just working with unsigned integer type, so I thought "Maybe using integer_sort() from boost/sort/spreadsort/integer_sort.hpp I can improve the speed" The Question is th...
more »

2017-04-01 09:04 (1) Answers

Correctly Writing a Range Based Constructor

I had a question to do with writing a range based constructor for a class but couldn't find the right phrasing to search for help on google. Suppose I am writing a simple class, like vector, which involves having a range based constructor that inser...
more »

2017-04-01 06:04 (1) Answers

Function not writing to txt file

I have a program that uses various structs and functions to read information into a struct from an output file, do something with it, and write to an output file if a condition is met. Everything is working properly, except the function that's suppos...
more »

2017-03-31 20:03 (1) Answers

Xcode, C++ and SFML. Library issues

I am trying to compile a simpel code that will show me a window by using Xcode with my mac. Everything seems to work and I get "Build Succeeded". But then nothing happens and I get the message: dyld: Library not loaded: @rpath/libsfml-audio.2.4.d...
more »

2017-03-30 15:03 (1) Answers

Input validation to only accept numbers

I'm creating program and it should only be allowed to accept numbers if a letter is entered it should re-ask the use to enter a number. When i run the code i get an error saying there's a problem with the break heres a copy of my code #include <...
more »

2017-03-30 08:03 (3) Answers

Cache locality with unique_ptr

I have a vector of custom classes (std::string just for example). The vector is large and I iterate through often, so I rely on cache locality. I also have one raw pointer which points at one of the vector elements. Now is the trick: The vector i...
more »

2017-03-29 22:03 (2) Answers

recursive function not called properly

I'm using a recursive method to convert a number into a representation in base of 7. However, after I run the program, the output string is identical to the input number. What's cause that the function is not called? #include <iostream> #inclu...
more »

2017-03-24 18:03 (1) Answers