Category : c++17

I have a class named Particle and i want to create a vector of particles. The problem is that my code is not drawing the objects stored in the vector. I tried different implementations, the first one was: std::vector<Particle> particles(10); and then i filled the vectors: for(size_t i = 0; i < 10; ++i) particles.emplace_back(new ..

Read more

If I have a class with std::atomic_bool or std::mutex member for example, and if I put this class inside std::variant, my g++ will complain with "no matching function for call to std::variant<….>". Now I have to declare my std::mutex member to be static. g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) Copyright (C) 2017 Free Software ..

Read more

I’m currently writing a genetic algorithm for my dissertation and running into an error implementing a parallel_for loop, its probably very obvious but I’m not sure how to solve the issue. void evaluate_population_fitness(vector<Chromosome *> population) { std::for_each(std::execution::par_unseq, population.begin(), population.end(), [](Chromosome *item) { item->setLength(1 / calculate_Chromosome_length(item)); }); } This is the actual parallel for loop here ..

Read more

Below is the snippet which is trying to check for the presence of a default constructor at compile time. Compiling this with clang version 11.0.0 Target: x86_64-apple-darwin19.6.0 Thread model: posix InstalledDir: /usr/local/opt/llvm/bin using options clang++ –std=c++17 -o test_default_ctor test_default_ctor.cpp #include <type_traits> template<typename T, typename = void> struct has_default_ctor_1 : std::false_type {}; template<typename T> struct has_default_ctor_1<T, ..

Read more

Consider the following code #include <type_traits> template<bool Test, class T, T val1, T val2> constexpr T if_v = std::conditional_t<Test, std::integral_constant<T, val1>, std::integral_constant<T, val2>>::value; int main() { constexpr size_t value1 = 123; constexpr size_t value2 = 456; constexpr bool test = (3 > 2); constexpr size_t r0 = if_v<test, size_t, value1, value2>; // = 123 return ..

Read more

An alternative to explicitly deleting constructors and assignment operators to disallow copying/moving/instancing a class is to introduce empty "tag" base classes and inherit them to achieve the same result. Here’s my implementation of such a solution: class Non_copyable { protected: Non_copyable() = default; ~Non_copyable() = default; Non_copyable(const Non_copyable&) = delete; Non_copyable& operator=(const Non_copyable&) = delete; ..

Read more