STD::FUNCTION C++ - c++11

I'm still learning Modern C++ and I would like to clarify STD:FUNCTION,
Here is my sample code that works fine :
#include <iostream>
#include <functional>
using namespace std;
int func(function<bool()> foo) {
return 2;
}
struct fee {
bool operator()() {
return true;
}
};
int main() {
cout << func(fee());
}
It will display "2" on the console.
What I am wondering is why this does not work. I changed bool operator()() to bool operator()(int i).
#include <iostream>
#include <functional>
using namespace std;
int func(function<bool()> foo) {
return 2;
}
struct fee {
bool operator()(int i) {
return true;
}
};
int main() {
cout << func(fee());
}
The error says:
In function 'int main()':
18:20: error: could not convert 'fee()' from 'fee' to 'std::function<bool()>'
What should be the right thing to do ?

In the second example, the fee operator() function now takes an int as a parameter.
Therefore you need to change
int func(function<bool()> foo) {
return 2;
}
to
int func(function<bool(int)> foo) {
return 2;
}
to reflect that.

Related

C++ Access member function using pointer

I am not able to access member function using pointer.Find the below code and Error message
The Error message is mentioned here
error: request for member ‘getstream’ in ‘* objA.A::getfunction()’, which is of non-class type ‘int’
ret = objA.getfunction()->getstream();
#include <iostream>
using namespace std;
class A {
public:
int *getfunction();
int getstream();
};
int* A::getfunction()
{
static int a;
a= getstream();
return &a;
}
int getstream()
{
return 1;
}
int main()
{
int *ret;
A objA;
ret = objA.getfunction()->getstream();
cout << ret;
return 0;
}
If you want to achieve a syntax like objA.getfunction()->getstream(); in your main function,
you can do it with class A implementation similar to this :
#include <iostream>
using namespace std;
// class A declaration
class A {
public:
// Nested class A::AFunction declaration
class AFunction {
public:
int getstream();
};
private:
AFunction *p_AFunction;
public:
A();
~A();
A::AFunction *getfunction();
}; // class A
// class A member function implementations
A::A() : p_AFunction(new AFunction()) {
}
A::~A() {
delete p_AFunction;
p_AFunction = nullptr;
}
A::AFunction *A::getfunction() {
return p_AFunction;
}
// Nested class A::AFunction member function implementations
int A::AFunction::getstream() {
return 1;
}
// main function
int main() {
A objA;
int ret = objA.getfunction()->getstream();
cout << ret;
return 0;
}
If you want A::getfunction() function to return a function pointer to a member function in class A, and then invoke it in main function, you can have a implementation similar to this :
#include <iostream>
using namespace std;
// class A declaration
class A {
public:
typedef int (A::*AMemberFuncPtr) ();
private:
AMemberFuncPtr fn_getstream;
public:
A();
A::AMemberFuncPtr getfunction();
private:
int getstream();
}; // class A
// class A member function implementations
A::A() : fn_getstream(&A::getstream) {
}
A::AMemberFuncPtr A::getfunction() {
return fn_getstream;
}
int A::getstream() {
return 1;
}
// main function
int main() {
A objA;
int ret = (objA.*objA.getfunction())();
cout << ret;
return 0;
}
Also see the answer to Function pointer to member function.

How to pass rvalue reference from caller to callee

Say I have code below
#include <iostream>
void foo(std::string && s) { std::cout << s; }
void bar(std::string && s) { foo(s); }
int main() {
bar("abc");
return 0;
}
I got compiling error:
error: cannot bind ‘std::string {aka std::basic_string}’ lvalue
to ‘std::string&& {aka std::basic_string&&}’ void
bar(std::string && s) { foo(s); }
Use std::move from <utility>:
#include <iostream>
#include <utility>
void foo(std::string && s) { std::cout << s; }
void bar(std::string && s) { foo(std::move(s)); }
int main() {
bar("abc");
return 0;
}
std::move is actually just a little bit of syntactical sugar, but is the common way of forwarding an rvalue reference.

Error in storing outer class object in inner class C++

I was implementing the ring buffer and have encountered an error. What does it mean to store a reference of outer class(class ring) object(m_ring) in inner class(class iterator) and when I remove the reference(&) the program compiles correctly but crashes. Please explain what is happening.(See the comment in Ring.h) Sorry for bad English.
// Ring.h
#ifndef RING.H
#define RING.H
#include <iostream>
using namespace std;
template<class T>
class ring {
unsigned int m_size;
int m_pos;
T *m_values;
public:
class iterator;
public:
ring(unsigned int size) : m_size(size), m_pos(0)
{
m_values = new T[m_size];
}
~ring()
{
delete[] m_values;
}
void add(const T &val)
{
m_values[m_pos] = val;
m_pos++;
m_pos %= m_size;
}
T& get(int pos)
{
return m_values[pos];
}
iterator begin()
{
return iterator(0, *this);
}
iterator end()
{
return iterator(m_size, *this);
}
};
template<class T>
class ring<T>::iterator {
int m_pos;
ring &m_ring; // Removing & gives garbage output.
public:
iterator(int pos, ring& aRing) : m_pos(pos), m_ring(aRing){}
bool operator!=(const iterator &other) const
{
return other.m_pos != m_pos;
}
iterator &operator++(int)
{
m_pos++;
return *this;
}
iterator &operator++()
{
m_pos++;
return *this;
}
T &operator*()
{
// return m_ring.m_values[m_pos];
return m_ring.get(m_pos);
}
};
#endif // RING
Driver program :
// Ring_Buffer_Class.cpp
#include <iostream>
#include "ring.h"
using namespace std;
int main()
{
ring<string> textring(3);
textring.add("one");
textring.add("two");
textring.add("three");
textring.add("four");
// C++ 98
for(ring<string>::iterator it = textring.begin(); it != textring.end(); it++)
{
cout << *it << endl;
}
cout << endl;
// C++11
for(string value : textring)
{
cout << value << endl;
}
return 0;
}
I also observed that removing ~ring() (Destructor) results into correct output.
Expected output :
four
two
three
four
two
three

How can I use std::result_of to return the function type instead of void

I am trying to get the return type of a function that I bind in. In this instance I was expecting to see the return type of GetFactorialResult (int).
#include <iostream>
#include <boost/core/demangle.hpp>
#include <typeinfo>
namespace
{
const int testNumber = 10;
int GetFactorialResult(int number)
{
if (number > 1)
{
return number * GetFactorialResult(number - 1);
}
else
{
return 1;
}
}
template <typename Func, typename... Args>
void Submit(Func&& func, Args&&... args)
{
auto boundTask = std::bind(std::forward<Func>(func), std::forward<Args>(args)...);
using ResultType = typename std::result_of<decltype(boundTask)()>::type;
char const * name = typeid( ResultType ).name();
std::cout << boost::core::demangle( name ) << std::endl;
}
}
int main()
{
Submit([](int number)
{
GetFactorialResult(number);
}, number);
return 0;
}
Output
void
0
When I print the type of boundTask, I see what I expect:
std::_Bind<\main::{lambda(int)#1} (int)> (the backslash doesnt
exist, but couldnt figure out how to display it without it).
I assume I am getting void because of I'm doing decltype(boundTask)(), but if I remove the parenthesis, it fails to compile.
I only have access to c++11 features.

Why the function don't work on the string?

#include <iostream>
#include <string>
using namespace std;
void chuli(string a)
{
a.erase(0,2);
}
int main()
{
string a = "012345";
a = chuli(a);
cout << a;
}
I am beginner in C++, I want to know why after this function, this string doesn't change. Is this something about the namespace?
The string is passed by value, so your action is applied to a copy of your object.
If you need to modify your value, you need to pass it by pointer or by reference:
void chuli(string &a)
{
a.erase(0,2);
}
void chuli(string *a)
{
a->erase(0,2);
}

Resources