Implementation of static constructor in c++ doesn't work - c++

http://ideone.com/1ohrsO
The push_back called inside the constructor of static_constructor, is not reflected. Why?
#include <iostream>
#include <vector>
#include<memory>
#include<string>
using namespace std;
class has_static_constructor
{
public:
friend class static_constructor;
static vector<int> v;
class static_constructor
{
public:
vector<int> * upt; //&v;
static_constructor()
{
cout<<"inside static_constructor";
upt = &has_static_constructor::v;
has_static_constructor::v.push_back(1);
has_static_constructor::v.push_back(20);
}
} ;
static std::unique_ptr<has_static_constructor::static_constructor> upt ;
};
unique_ptr<has_static_constructor::static_constructor> has_static_constructor::upt(new has_static_constructor::static_constructor());
vector< int > has_static_constructor::v(2,100);
int main() {
// your code goes here
for (std::vector<int>::const_iterator i = has_static_constructor::v.begin(); i != has_static_constructor::v.end(); ++i)
{ std::cout << *i << ' ';
cout<<"\n I was here\n";
}
return 0;
}
Output:
inside static_constructor100
I was here
100
I was here

static_constructor() is called before has_static_constructor::v initialization.
Move
unique_ptr<has_static_constructor::static_constructor> has_static_constructor::upt(new has_static_constructor::static_constructor());
after
vector< int > has_static_constructor::v(2,100);
to have expected behaviour.
But better avoid those global entirely.

You might want to have a look at this way of ordering the code. It removes all initialisation-order dependencies, and in my view neatly separates the public interface from the internal implementation of the static data.
#include <iostream>
#include <vector>
class has_static_constructor
{
// note - all private
struct static_data {
static_data()
: _v(2, 100)
{
_v.push_back(1);
_v.push_back(20);
}
std::vector<int> _v;
};
static static_data& statics() {
static static_data sd;
return sd;
}
// public interface
public:
static std::vector<int>& v() { return statics()._v; }
};
auto main() -> int
{
for (const auto& i : has_static_constructor::v())
{
std::cout << i << std::endl;
}
return 0;
}
expected output:
100
100
1
20

Related

Nested map with same class

I would like to write a class that stores recursively the same class in a map.
I have the following source codes:
/* A.hpp */
#include <iostream>
#include <string>
#include <map>
class A
{
private:
int a;
std::map<int, A> data;
bool finishCycle;
public:
A(const int& input ) : a(input), finishCycle(false)
{
func1();
}
void func1();
};
/* A.cpp */
void A::func1()
{
A tmp(*this);
while(!finishCycle)
{
a--;
if (a == 0)
finishCycle=true;
else
func1();
}
data.emplace(tmp.a, tmp);
}
/* main.cpp*/
#include "A.hpp"
int main(int argCount, char *args[])
{
A myObj1 (3);
std::cout << "tst" << std::endl;
return 0;
}
This is putting all the entries in the main map. I would like to have it in a nested sequence:
first entry: <1, A>
inside first entry: <2,A>
inside inside first entry <3,A>
How can I change the script to do this? The nested loops still make me confused.
Maybe this is what you want?
#include <iostream>
#include <map>
class A
{
private:
std::map<int, A> data;
public:
A(int n, int i = 0) {
if (n == i) {
return;
}
data.emplace( i+1, A(n, i + 1) );
}
void test() {
if (!data.empty()) {
std::cout << data.begin()->first << "\n";
data.begin()->second.test();
}
}
};
int main()
{
A a(4);
a.test();
}
I came up with:
#include <iostream>
#include <map>
class A
{
private:
int a;
std::map<int, A> data;
bool finishCycle;
public:
A(const int& input ):a(input), finishCycle(false)
{
func1(*this);
}
void func1(A& tmp);
};
void A::func1(A& tmp)
{
A tmp2(tmp);
tmp2.a--;
while(!finishCycle)
{
if (tmp2.a == 0)
{
(*this).finishCycle=true;
}
else
{
func1(tmp2);
}
}
tmp.data.emplace(tmp2.a, tmp2);
}
int main(int argCount, char *args[])
{
A myObj1 (4);
std::cout << "tst" << std::endl;
return 0;
}

C++ unordered_map weird size allocation

I am experimenting with this code with a lockfree programming and running into weird problems.
This is a dinning philosophers problem, and I am trying to implement that in a lockfree way.
I am reserving the size of unordered_map and making sure that it is not relocating objs during runtime. But unfortunately what I see that during runtime size of Philosopher::umap is weirdly assigned, (in example below, I am restricting it to 10, but during runtime it is changing to 30-40). Can someone help me understand what is going on here.
#include <iostream>
#include <thread>
#include <string>
#include <vector>
#include <memory>
#include <atomic>
#include <chrono>
#include <tuple>
#include <unordered_map>
class Eater {
public:
virtual void Eating() = 0;
~Eater() {
}
};
class Dreamer {
public:
virtual void Dreaming() = 0;
~Dreamer() {}
};
class Gamer {
public:
virtual void Gaming() = 0;
~Gamer() {
}
};
class Philosopher : public Dreamer, public Eater {
private:
const uint32_t id_;
public:
static std::atomic<uint32_t> gen;
static std::unordered_map<uint32_t, std::tuple<uint32_t, uint32_t>> umap;
Philosopher()
: id_(++gen) {
umap.emplace(id_, std::make_tuple(0,0));
}
Philosopher(const Philosopher& that) : id_(that.id_) {
}
Philosopher(Philosopher&& that) : id_(that.id_) {
}
virtual void Eating() override {
umap[id_] = std::make_tuple(1+std::get<0>(umap[id_]), std::get<1>(umap[id_])); // this is failing - despite, seperate thread working on separate memory location
}
virtual void Dreaming() override {
umap[id_] = std::make_tuple(std::get<0>(umap.at(id_)), 1+std::get<1>(umap.at(id_))); // this is failing - despite, seperate thread working on separate memory location
}
~Philosopher() {
}
};
std::unordered_map<uint32_t, std::tuple<uint32_t, uint32_t>> Philosopher::umap;
std::atomic<uint32_t> Philosopher::gen(0u);
class Engine : public Gamer {
private:
uint32_t m_count_philosophers_;
std::vector<std::shared_ptr<std::thread>> m_vthread_;
public:
Engine(uint32_t n)
: m_count_philosophers_(n) {
m_vthread_.reserve(n);
}
virtual void Gaming() override {
std::atomic<uint32_t> counter(0u);
while(counter++ < m_count_philosophers_) {
m_vthread_.emplace_back(std::make_shared<std::thread>([&]() mutable {
Philosopher philosopher;
std::chrono::duration<double> elapsed_seconds;
auto start = std::chrono::steady_clock::now();
while(elapsed_seconds.count() < 2) {
philosopher.Eating();
philosopher.Dreaming();
auto finish = std::chrono::steady_clock::now();
elapsed_seconds = finish - start;
}
}));
}
for(auto &iter : m_vthread_) {
iter->join();
}
}
};
int main() {
auto N = 10u;
Philosopher::umap.reserve(N); // there are no more than N elements in the unordered_map
Engine eng(N);
eng.Gaming();
for (auto &i : Philosopher::umap)
{
std::cout << "phi: " << i.first << " eating: " << std::get<0>(i.second) << ", dreaming: " << std::get<1>(i.second) << "\n";
}
return 0;
}

How to transfer a class type to a function template for new operation as a parameter?

I have a piece of c++ code:
#include <iostream>
#include <string>
#include <map>
static counter = 0;
class Probe
{
private:
int supply_;
Probe(const Probe&);
public:
Probe()
{
supply_ = 10000;
}
int get_supply()
{
return supply_;
}
};
/********************************************************************************
template<class T> T Create(int counter, T& produced)
{
produced[counter] = new ; // ??????????????????????????????????????
return produced;
}
************************************************************************************/
std::map<int, Probe*> CreatInitWorkers(int counter, std::map<int, Probe*> &init_workers)
{
init_workers[counter] = new Probe();
return init_workers;
}
int main()
{
std::map<int, Probe*> workers;
for (int i = 0; i < 12; i++)
{
workers = CreatInitWorkers(worker_counter++, workers);
}
for (auto it : workers)
{
std::cout << it.first << std::endl;
}
}
I want to create a template function (as it shows between the stars) like the CreatInitWorkers function. But I don't know how to transfer the Probe class to the new operation because for my program there are still other classes needed to be there. Is there any way can do it? Thanks.
Something along these lines:
template<class T> T Create(int counter, T& produced)
{
using C = std::remove_pointer_t<std::decay_t<decltype(produced[counter])>>;
produced[counter] = new C();
return produced;
}
Demo

A vector for different classes

I have a class with a vector I'd like to fill with one of two types of class, selected by the user. Let's call my classes option1 and option2
What I'd like to do it something like
class storage_class
{
public:
storage_class(int sel, int n)
{
if(sel == 1)
for(int i = 0; i < n; i++)
my_store.push_back(std::make_unique<option1>());
else if(sel == 2)
for(int i = 0; i < n; i++)
my_store.push_back(std::make_unique<option2>());
}
private:
// Something like this but that actually works
std::vector<T> my_store;
};
Then I'd like to use it like this, or something similar, so there's no need to modify this usage dependent on the option chosen.
int main()
{
storage_class store(1);
int n_iterations = 4;
for(int i = 0; i < n_iterations; i++)
{
store.my_store[i]->create_data();
}
}
The classes option1 and option2 will be mathematical simulations that will be creating data and themselves store this data in a vector that are members of the class.
I want to store multiple instances of either option in a vector and then manipulate them from there. I can use C++17.
As you have c++17 in use, you can simply use a std::variant as type for the container which itself can keep all types you want to have.
Example:
class A { public: void Do() { std::cout << "A::Do" << std::endl; } };
class B { public: void Go() { std::cout << "B::Go" << std::endl; } };
template<class... Ts> struct funcs : Ts... { using Ts::operator()...; };
template<class... Ts> funcs(Ts...) -> funcs<Ts...>;
int main()
{
std::vector<std::variant<A,B>> vec;
vec.push_back(A{});
vec.push_back(B{});
for ( auto& el: vec)
{
std::visit( funcs{ [](A& a){ a.Do(); }, [](B& b) { b.Go(); } }, el);
}
}
Output:
A::Do
B::Go
The classes are fully independent and the methods can be simply called with std::visit and passing a callable object herein. I provide a simple funcs implementation, which simply collect all callable entities to simplify to interface the call to different methods of different unrelated classes here.
As std::variant is some kind of a tagged union, it needs the storage for the biggest type you have in use. If this wastes to much memory, you can store a pointer to the instance instead, maybe with std::unique_ptr or std::shared_ptr if you like some assistance for memory management ;)
Here is an example that tries to stay as close to your example as it can using a template parameter on class storage_class. See working version here. I've added only option1 and made the member my_store public as you access it in your main function.
#include <memory>
#include <vector>
#include <iostream>
struct option1{
void create_data(){ std::cout << "created\n"; }
};
template<typename T>
class storage_class
{
public:
storage_class(int n)
{
for(int i = 0; i < n; i++)
my_store.push_back(std::make_unique<T>());
}
std::vector<std::unique_ptr<T>> my_store;
};
int main()
{
storage_class<option1> store(4);
int n_iterations = 4;
for(int i = 0; i < n_iterations; i++)
{
store.my_store[i]->create_data();
}
}
another option would be to use std::variant. See workign version here.
#include <memory>
#include <vector>
#include <variant>
#include <iostream>
struct option1{
void create_data(){ std::cout << "created 1\n"; }
};
struct option2{
void create_data(){ std::cout << "created 2\n"; }
};
class storage_class
{
public:
using option = std::variant<std::unique_ptr<option1>,std::unique_ptr<option2>>;
storage_class(int sel, int n)
{
if(sel == 0)
for(int i = 0; i < n; i++)
my_store.push_back(option(std::make_unique<option1>()));
else if(sel == 1)
for(int i = 0; i < n; i++)
my_store.push_back(option(std::make_unique<option2>()));
}
std::vector<option> my_store;
};
int main()
{
storage_class store(1, 4);
int n_iterations = 4;
for(int i = 0; i < n_iterations; i++)
{
std::get<1>(store.my_store[i])->create_data();
}
}
Standard way is to make option1 and option2 derived classes from a base_class which seems consistent with your sample main(). Using a generic Factory class template, here is an example:
#include <functional>
#include <iostream>
#include <memory>
#include <unordered_map>
#include <vector>
// Generic Factory class template
template<typename K,typename T,typename... Ts>
class Factory
{
using Map = std::unordered_map<K, std::function<std::unique_ptr<T>(Ts...)>>;
const Map mMap;
public:
Factory(Map&& map):mMap(std::move(map)) { }
std::unique_ptr<T> operator()(const K& key, Ts... args) const
{
const typename Map::const_iterator itr = mMap.find(key);
return itr == mMap.cend() ? nullptr : itr->second(std::forward<Ts>(args)...);
}
};
class base_class
{
public:
virtual void create_data() = 0;
};
class option1 : public base_class
{
public:
void create_data() override
{
std::cout << "I'm option1." << std::endl;
}
};
class option2 : public base_class
{
public:
void create_data() override
{
std::cout << "I'm option2." << std::endl;
}
};
class storage_class
{
using SimulationFactory = Factory<int,base_class>; // Optionally add constructor parameter types
const SimulationFactory simulation_factory; // This can be made static const.
public:
storage_class(int sel, int n)
: simulation_factory(
{ { 1, []() { return std::make_unique<option1>(); } }
, { 2, []() { return std::make_unique<option2>(); } }
})
{
for (int i = 0; i < n; i++)
my_store.push_back(simulation_factory(sel));
}
std::vector<std::unique_ptr<base_class>> my_store;
};
int main()
{
int n_iterations = 4;
storage_class store(1, n_iterations);
for(int i = 0; i < n_iterations; i++)
{
store.my_store[i]->create_data();
}
}
This compiled for me on linux using g++ -std=c++17 main.cc.
There are improvements that can be made to this code, but I copied your main() function in order to illustrate the basic idea(s). Hope that helps.
Edit 21 Sept 2018 - Example of how to pass parameters into constructors.
File: factory.h
#pragma once
#include <functional>
#include <memory>
#include <unordered_map>
// Generic Factory class template
template<typename K,typename T,typename... Ts>
class Factory
{
using Map = std::unordered_map<K, std::function<std::unique_ptr<T>(Ts...)>>;
const Map mMap;
public:
Factory(Map&& map):mMap(std::move(map)) { }
std::unique_ptr<T> operator()(const K& key, Ts... args) const
{
const typename Map::const_iterator itr = mMap.find(key);
return itr == mMap.cend() ? nullptr : itr->second(std::forward<Ts>(args)...);
}
};
File: main.cc
#include "factory.h"
#include <iostream>
#include <string>
#include <vector>
class base_class
{
public:
virtual void create_data() = 0;
};
class option1 : public base_class
{
const double mD;
public:
option1(double d)
: mD(d)
{ }
void create_data() override
{
std::cout << "I'm option1: mD("<<mD<<')' << std::endl;
}
};
class option2 : public base_class
{
const double mD;
public:
option2(double d)
: mD(d)
{ }
void create_data() override
{
std::cout << "I'm option2: mD("<<mD<<')' << std::endl;
}
};
class storage_class
{
using SimulationFactory = Factory<int,base_class,double>; // Optionally add constructor parameter types
const SimulationFactory simulation_factory; // This can be made static const.
public:
storage_class(int sel, int n)
: simulation_factory(
{ { 1, [](double d) { return std::make_unique<option1>(d); } }
, { 2, [](double d) { return std::make_unique<option2>(d); } }
})
{
for (int i = 0; i < n; i++)
my_store.push_back(simulation_factory(sel,static_cast<double>(i)));
}
std::vector<std::unique_ptr<base_class>> my_store;
};
int main()
{
int n_iterations = 4;
storage_class store1(1, n_iterations);
storage_class store2(2, n_iterations);
for(int i = 0; i < n_iterations; i++)
{
store1.my_store[i]->create_data();
store2.my_store[i]->create_data();
}
}
Output:
I'm option1: mD(0)
I'm option2: mD(0)
I'm option1: mD(1)
I'm option2: mD(1)
I'm option1: mD(2)
I'm option2: mD(2)
I'm option1: mD(3)
I'm option2: mD(3)

Issues creating a vector of class object in c++

I created the following class
#include "cliques.h"
#include "vector"
#include <iostream>
using namespace std;
cliques::cliques(){
}
cliques::cliques(int i) {
clique.push_back(i);
clique_prob = 1;
mclique_prob = 1;
}
cliques::cliques(const cliques& orig) {
}
cliques::~cliques() {
}
void cliques::addvertex(int i) {
clique.push_back(i);
}
double cliques::getclique_prob() const {
return clique_prob;
}
double cliques::getMaxclique_prob() const {
return mclique_prob;
}
void cliques::showVertices() {
for (vector<int>::const_iterator i = clique.begin(); i !=clique.end(); ++i)
cout << *i << ' ';
cout << endl;
}
vector<int> cliques::returnVector() {
return clique;
}
void cliques::setclique_prob(double i) {
clique_prob = i;
}
void cliques::setMaxclique_prob(double i) {
mclique_prob = i;
}
Here's the header file
#include "vector"
#ifndef CLIQUES_H
#define CLIQUES_H
class cliques {
public:
void addvertex(int i);
cliques();
cliques(int i);
cliques(const cliques& orig);
virtual ~cliques();
double getclique_prob() const;
double getMaxclique_prob() const;
void showVertices();
std::vector<int> returnVector();
void setclique_prob(double i);
void setMaxclique_prob(double i);
private:
float clique_prob;
float mclique_prob;
std::vector <int> clique;
};
#endif /* CLIQUES_H */
I want to create a vector of these objects in order to implement a heap
int main(int argc, char** argv) {
cliques temp(1);
cliques temp1(2);
temp.setclique_prob(0.32);
temp.setclique_prob(0.852);
temp.showVertices();
temp1.showVertices();
vector <cliques> max_heap;
max_heap.push_back(temp);
max_heap.push_back(temp1);
double x =max_heap.front().getclique_prob();
cout<<"prob "<<x<<endl;
cliques y = max_heap.front();
y.showVertices();
//make_heap (max_heap.begin(),max_heap.end(),max_iterator());
//sort_heap (max_heap.begin(),max_heap.end(),max_iterator());
return 0;
}
For reasons unknown to me none of my class functions work properly after i create my vector, meaning that while the following function works as intended
temp.showVertices()
the next one doesn't,
y.showVertices()
You miss implementation for
cliques::cliques(const cliques& orig) {
}
STL vector uses copy constructor inside when you add values to it. As your cliques class does not allocate any memory, you can just remove the copy constructor from the code and compiler will generate one for you.