How to move a unique_ptr from one set to another? (C++) - c++

The code below is giving the error: Call to deleted constructor of 'std::unique_ptr<int>' 'unique_ptr' has been explicitly marked deleted here passing argument to parameter 'item' here.
Could someone please explain why this is? I would have thought everything would be fine because I'm using std::move in the call to foo.add.
#include <iostream>
#include <memory>
#include <set>
class Foo {
public:
void add(std::unique_ptr<int> item) {
set.emplace(std::move(item));
}
private:
std::set<std::unique_ptr<int>> set;
};
int main() {
Foo foo;
std::set<std::unique_ptr<int>> set;
set.emplace(std::make_unique<int>(1));
set.emplace(std::make_unique<int>(2));
set.emplace(std::make_unique<int>(3));
for (auto &item : set) {
foo.add(std::move(item)); // error on this line
}
return 0;
}

Use c++ 17 extract() function.
example
#include <set>
#include <memory>
#include <iostream>
int main() {
auto s = std::set<std::unique_ptr<int>>{};
s.insert(std::make_unique<int>(10));
std::cout << s.size() << "\n";
auto it = s.extract(s.begin());
// Pointer type here just for clarification
std::unique_ptr<int> new_ptr = std::move(it.value());
std::cout << s.size() << "\n";
std::cout << *new_ptr << "\n";
}
Then instead of your for each loop you might use a while loop:
while (!set.empty()) {
auto it = set.extract(set.begin());
foo.add(std::move(it.value());
}

Related

Expand variadic arguments in initializer list fails

I try to create a simple program in which I want to create vector of future arguments.
I created a wrapper function which is used to submit lambda functions and stores internally in a vector the future objects
I use an intermediate step in which I create an initiliazer_list using variadic arguments . But fails to compile. I try to use to call a function in order to push the elements in the vector and fails to compile as well
Below is the code
#include <iostream>
#include <thread>
#include <future>
#include <functional>
#include <cstdlib>
#include <chrono>
#include <initializer_list>
using namespace std;
using FunctPtr = function<int(int, int) >;
using FutureInt = future<int>;
using AsyncVector = vector<FutureInt>;
AsyncVector asyncVec;
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
auto initList {pFunc... };
for (auto & element : initList)
{
asyncVec.emplace_back(async(launch::async, element,4,5));
}
}
int main()
{
int a;
int b;
auto addPtr = [](int x, int y)->int
{
std::cout << "add :" << x + y << std::endl;
return x + y;
};
auto multPtr = [](int x, int y)->int
{
std::cout << "mult :" << x * y << std::endl;
return x * y;
};
// submit(add,4,5);
submit(addPtr, multPtr);
for (auto & v : asyncVec)
{
std::cout << "Wait for " << v.get() << std::endl;
}
}
Yes, they are of different types so cannot be in the same init-list easily.
Your best options should probably be:
Either push them all into asyncVec in the same fold-expression.
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
(asyncVec.emplace_back(async(launch::async, std::move(pFunc), 4, 5)), ...);
}
Or, if they all are of the same signature, type-erase them, like keeping them in an array of std::function.
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
for (auto &&element: {std::function<int(int, int)>(std::move(pFunc))...})
{
asyncVec.emplace_back(async(launch::async, std::move(element), 4, 5));
}
}
(I have specified function signature explicitly though compiler should be able to deduce it.)
Or, if all closures are captureless and of the same signature, simply cast them to the same type when calling submit:
using SameType = int (*)(int, int);
submit(static_cast<SameType>(addPtr), static_cast<SameType>(mulPtr));
This way your original submit should work as is.

C++ compiling error when calling a constructor within other constructor's call [duplicate]

This question already has an answer here:
Why "Foo f(Bar());" can be a declaration of a function that takes type Bar and returns type Foo? [duplicate]
(1 answer)
Closed 4 years ago.
I have a C++ code that seems to be confusing a class contructor like A::A(B b) with a constructor that receives a function pointer, like A::A(B (*)()). Let me explain:
The following code compiles:
#include <iostream>
#include <cstring>
#include <vector>
struct Item {
Item() {
std::cout << "ITEM::Normal constructor\n";
}
};
struct Container {
Container(Item i) {
std::cout << "CONTAINER::Normal constructor\n";
}
void doSomething() {
std::cout << "Do something\n";
}
};
int main() {
Container c3(Item());
return 0;
}
But if I add a call to B::doSomething(), like the following code, I obtain a compiler error that I don't understand:
#include <iostream>
#include <cstring>
#include <vector>
struct Item {
Item() {
std::cout << "ITEM::Normal constructor\n";
}
};
struct Container {
Container(Item i) {
std::cout << "CONTAINER::Normal constructor\n";
}
void doSomething() {
std::cout << "Do something\n";
}
};
int main() {
Container c3(Item());
c3.doSomething();
return 0;
}
The compiling error is:
main.cpp: In function ‘int main()’:
main.cpp:23:6: error: request for member ‘doSomething’ in ‘c3’, which is of non-class type ‘Container(Item (*)())’
c3.doSomething();
It's a veiled most vexing parse issue:
Container c3(Item());
declares a function prototype, and your helpful compiler issues the appropriate diagnostic.
Container c3{Item()};
is the fix.

how can I list-initialize my own class?

I want my own class can be list-initialized like vector:
myClass a = {1, 2, 3};
How can I do that using C++11 capabilities?
C++11 has a notion of initializer lists. To use it, add a constructor which accepts a single argument of type std::initializer_list<T>. Example:
#include <vector>
#include <initializer_list>
#include <iostream>
struct S
{
std::vector<int> v_;
S(std::initializer_list<int> l)
: v_(l)
{
std::cout << "constructed with initializer list of length " << l.size();
}
};
int main()
{
S s = { 1, 2, 3 };
return 0;
}
An initializer_list can be (like other STL containers) iterated and the size can be queried. This allows you to do as pointed in the other answer by yuyoyuppe to immediately feed it to a vector. But you may have other intents for your initialization and do something like execute an operation directly on the items of the list without copying.
#include <initializer_list>
#include <iostream>
#include <algorithm>
struct S
{
int thesum;
int items;
S(std::initializer_list<int> l) :
thesum(std::accumulate(l.begin(), l.end(), 0)),
items(l.size())
{}
};
int main()
{
S s = { 1, 2, 3 };
std::cout << s.items << std::endl;
std::cout << s.thesum << std::endl;
return 0;
}
This prints
3
6
See:
http://www.cplusplus.com/reference/initializer_list/initializer_list/?kw=initializer_list
http://en.cppreference.com/w/cpp/utility/initializer_list
-

Can boost accumulators be used as class members

I'm trying to use a boost accumulator to calculate a rolling mean. When I declare the variable inline like this:
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
using namespace boost::accumulators;
int main()
{
// Define rolling_mean accumulator
accumulator_set<double, stats<tag::rolling_mean > > acc(tag::rolling_window::window_size = 5);
// push in some data ...
acc(1.2);
acc(2.3);
acc(3.4);
acc(4.5);
// Display the results ...
std::cout << "Mean: " << rolling_mean(acc) << std::endl;
return 0;
}
It works just fine. When I declare the accumulator as a member of a class like so:
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
using namespace boost::accumulators;
class DoMean {
private:
accumulator_set<double, stats<tag::rolling_mean > > m_acc(tag::rolling_window::window_size = 5);
public:
void addData(double val) {
this->m_acc(val);
}
double getMean(void) {
return rolling_mean(this->m_acc);
}
};
int main()
{
// Define an accumulator set for calculating the mean and the
// 2nd moment ...
DoMean meaner;
meaner.addData(1.2);
meaner.addData(2.3);
meaner.addData(3.4);
meaner.addData(4.5);
// push in some data ...
// Display the results ...
std::cout << "Mean: " << meaner.getMean() << std::endl;
return 0;
}
It fails, giving the compiler errors:
accumulators::tag::rolling_window::window_size is not a type
...blah blah, many type template errors etc.
The correct solution to this problem is this:
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
using namespace boost::accumulators;
class DoMean {
private:
accumulator_set<double, stats<tag::rolling_mean > > m_acc;
public:
DoMean(void): m_acc(tag::rolling_window::window_size = 5) {}
void addData(double val) {
this->m_acc(val);
}
double getMean(void) {
return rolling_mean(this->m_acc);
}
};
int main()
{
// Define an accumulator set for calculating the mean and the
// 2nd moment ...
DoMean meaner;
meaner.addData(1.2);
meaner.addData(2.3);
meaner.addData(3.4);
meaner.addData(4.5);
// push in some data ...
// Display the results ...
std::cout << "Mean: " << meaner.getMean() << std::endl;
return 0;
}
Notice that the initialization of m_acc has been moved from inline with it's declaration, into the initialization list. This solves all the compiler errors. In fact, if we think about what's happening here, the reason that the initial attempt at using the accumulator in a class fails is because ISO c++ forbids initialization of members inline.
We can demonstrate this with another simple class:
#include <iostream>
class TestInit {
public:
int m_init = 10;
};
int main() {
TestInit inits;
std::cout << "The value: " << inits.m_init << std::endl;
}
Now the compiler gives us a helpful message:
/home/me/prog/cpp/acctest/testinit.cxx:5:16: error: ISO C++ forbids initialization of member m_init [-fpermissive]
/home/me/prog/cpp/acctest/testinit.cxx:5:16: error: making m_init static [-fpermissive]
/home/me/prog/cpp/acctest/testinit.cxx:5:16: error: ISO C++ forbids in-class initialization of non-const static member m_init

A pointer to a bound function may only be used to call the function

I'm working on a homework assignment for my C++ class and have ran across a problem that I cannot figure out what I am doing wrong.
Just to note, the separation of the files is necessary and I realize this would be much easier if I just made a structure AttackStyles inside the main and forgo the additional class file altogether.
The base of my problem is that I cannot seem to be able to loop through an array of classes and pull out base data. Here is the code:
// AttackStyles.h
#ifndef ATTACKSTYLES_H
#define ATTACKSTYLES_H
#include <iostream>
#include <string>
using namespace std;
class AttackStyles
{
private:
int styleId;
string styleName;
public:
// Constructors
AttackStyles(); // default
AttackStyles(int, string);
// Destructor
~AttackStyles();
// Mutators
void setStyleId(int);
void setStyleName(string);
// Accessors
int getStyleId();
string getStyleName();
// Functions
};
#endif
/////////////////////////////////////////////////////////
// AttackStyles.cpp
#include <iostream>
#include <string>
#include "AttackStyles.h"
using namespace std;
// Default Constructor
AttackStyles::AttackStyles()
{}
// Overloaded Constructor
AttackStyles::AttackStyles(int i, string n)
{
setStyleId(i);
setStyleName(n);
}
// Destructor
AttackStyles::~AttackStyles()
{}
// Mutator
void AttackStyles::setStyleId(int i)
{
styleId = i;
}
void AttackStyles::setStyleName(string n)
{
styleName = n;
}
// Accessors
int AttackStyles::getStyleId()
{
return styleId;
}
string AttackStyles::getStyleName()
{
return styleName;
}
//////////////////////////////////////////////
// main.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "attackStyles.h"
using namespace std;
int main()
{
const int STYLE_COUNT = 3;
AttackStyles asa[STYLE_COUNT] = {AttackStyles(1, "First"),
AttackStyles(2, "Second"),
AttackStyles(3, "Third")};
// Pointer for the array
AttackStyles *ptrAsa = asa;
for (int i = 0; i <= 2; i++)
{
cout << "Style Id:\t" << ptrAsa->getStyleId << endl;
cout << "Style Name:\t" << ptrAsa->getStyleName << endl;
ptrAsa++;
}
system("PAUSE");
return EXIT_SUCCESS;
}
My question is why do I get the error:
"a pointer to a bound function may only be used to call the function"
on both ptrAsa->getStyleId and ptrAsa->getStyleName?
I cannot figure out what is wrong with this!
You are missing () around the function calls. It should be ptrAsa->getStyleId().
You are missing parenthesis on both calls, it should be
ptrAsa->getStyleId()
to call the function.
ptrAsa->getStyleId
is used to refer to a member value / attribute.
You need to invoke the function, not merely reference it:
std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n";
std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n";
You are Forgot to put () in last in Your Function(ptrAsa->getStyleId ) Calling with arrow operator.