Fixing a parameter when using std::bind - c++11

This code doesn't compile, I don't get why:
struct C { int a;};
void foo(C c, int s)
{
cout << c.a << s;
}
int main()
{
std::function<void(C,int)> call = std::bind(&foo,std::placeholders::_1,5);
C c;
c.a = 5;
call(c);
return 0;
}
I get:
No match for call to std::function<void(C,int)> (C&)

The bind() expression std::bind(&foo, _1, 5) produces a unary function. You try to use a unary function to initialize a binary std::function<void(c, int)>. Did you mean to use something like this?
std::function<void(C)> call = std::bind(&foo, _1, 5);

Related

Is it possible in C++11 to combine functions into a new function?

This is more a kind of theoretical question. Is it possible in C++11 to combine functions into a new function? For example :
auto f = [](int i){return i * 2;};
auto g = [](int i){return i + 10;};
So this works:
auto c = f(g(20)); // = 60
But I want an object that stores the combination, like
auto c = f(g);
std::cout << c(20) << std::endl; //prints 60
Edit:
Additionally what i want to create is a function a, which you can give a function b and an int n, and which returns the n'th combination of the given function b. For example (not compilable)
template<typename T>
auto combine(T b, int i) -> decltype(T)
{
if (i == 0)
return b;
return combine(b, i - 1);
}
auto c = combine(f, 2); //c = f(f(f(int)))
A first attempt:
template<class First, class Second>
auto compose( Second&& second, First&& first ) }
return [second = std::forward<Second>(second), first=std::forward<First>(first)]
(auto&&...args)->decltype(auto) {
return second( first( decltype(args)(args)... ) );
};
}
template<class A, class B, class...Rest>
auto compose(A&& a, B&& b, Rest&&... rest) {
return compose( compose(std::forward<A>(a), std::forward<B>(b)), std::forward<Rest>(rest)... );
}
template<class A>
std::decay_t<A> compose(A&& a) {
return std::forward<A>(a);
}
in C++14. Now, this isn't perfect, as the pattern doesn't work all that well in C++.
To do this perfectly, we'd have to take a look at compositional programming. Here, functions interact with an abstract stack of arguments. Each function pops some number of arguments off the stack, then pops some number back on.
This would allow you do do this:
compose( print_coord, get_x, get_y )
where get_x and get_y consume nothing but return a coordinate, and print_coord takes two coordinates and prints them.
To emulate this in C++, we need some fancy machinery. Functions will return tuples (or tuple-likes?), and those values will be "pushed onto the argument stack" logically.
Functions will also consume things off this argument stack.
At each invocation, we unpack the current tuple of arguments, find the longest collection that the function can be called with, call it, get its return value, unpack it if it is a tuple, and then stick any such returned values back on the argument stack.
For this more advanced compose to compose with itself, it then needs SFINAE checks, and it needs to be able to take a invokable object and a tuple of arguments and find the right number of arguments to call the invokable object with, plus the left-over arguments.
This is a tricky bit of metaprogramming that I won't do here.
The second part, because I missed it the first time, looks like:
template<class F>
auto function_to_the_power( F&& f, unsigned count ) {
return [f=std::forward<F>(f),count](auto&& x)
-> std::decay_t< decltype( f(decltype(x)(x)) ) >
{
if (count == 0) return decltype(x)(x);
auto r = f(decltype(x)(x));
for (unsigned i = 1; i < count; ++i) {
r = f( std::move(r) );
}
return r;
};
}
This uses no type erasure.
Test code:
auto f = [](int x){ return x*3; };
auto fs = std::make_tuple(
function_to_the_power( f, 0 ),
function_to_the_power( f, 1 ),
function_to_the_power( f, 2 ),
function_to_the_power( f, 3 )
);
std::cout << std::get<0>(fs)(2) << "\n";
std::cout << std::get<1>(fs)(2) << "\n";
std::cout << std::get<2>(fs)(2) << "\n";
std::cout << std::get<3>(fs)(2) << "\n";
prints:
2
6
18
54
You can write something along the lines of:
#include <functional>
#include <iostream>
template<class F>
F compose(F f, F g)
{
return [=](int x) { return f(g(x)); };
}
int main()
{
std::function<int (int)> f = [](int i) { return i * 2; };
std::function<int (int)> g = [](int i) { return i + 10; };
auto c = compose(f, g);
std::cout << c(20) << '\n'; // prints 60
}
The code can be simply extended to cover the second half of the question:
template<class F>
F compose(F f, unsigned n)
{
auto g = f;
for (unsigned i = 0; i < n; ++i)
g = compose(g, f);
return g;
}
int main()
{
std::function<int (int)> h = [](int i) { return i * i; };
auto d = compose(h, 1);
auto e = compose(h, 2);
std::cout << d(3) << "\n" // prints 81
<< e(3) << "\n"; // prints 6561
}
NOTE. Here using std::function. It isn't a lambda but wraps a lambda with a performance cost.

return the answer from "void" to another function

I got question how can i return the answer to display because i kept getting an error that void can't return a variable.
How can i send it to my display function.
void Rational::add(const Rational&h2)
{
int num = 0;
int dem = 0;
add(h2);
int P = num * h2.dem + h2.num*dem;
int Q = dem*h2.dem;
}
void display() const; // _p:_q
{
if (Q == 1) // e.g. fraction 2/1 will display simply as 2
cout << P << endl;
else
cout <<P << "/" << Q << endl;
}
void is the return type of the function you defined, void also means that you're returning nothing. if you had a function "int Rational::add(**)" then you can return a int from the function.
From what I can see you have a few problems:
You call add() from inside your add() function, this will keep looping until you have a stack overflow
From what I can see you have a variable Q and P and you want to display them, display isn't a function that belongs to that class so it can't access the variables Q and P which I assume are defined in your header.
If you do define Q and P inside your header then your function add() isn't writing to them. When you say int Q and int P in your add() function you're defining a new variable with the same name, writing to this local variable isn't going to change the Q and P you may be intending to write to. To solve this you simply don't define Q and P again and just write it without the int.
P = num * h2.dem + h2.num*dem;
Q = dem*h2.dem;

How can I add a reference to a value-type in vala

In c++ I can add a reference to a value type, for example :
int a = 12;
int &b = a;
a--;
cout << "a = " << a << ", b = " << b << endl;
Will give :
a = 11, b = 11
Is there a way to do the same in vala without using pointers ?
Is there a way to do the same in vala
Yes.
without using pointers ?
No.
If, however, you are passing them to a function, you can use a ref parameter:
void decrement (ref value) {
value--;
}
void do_stuff () {
int a = 12;
decrement (ref a);
assert (a == 11);
}

Passing lambda as argument is not working properly

I am learning about lambdas and I don't understand why passing the lambda as a Predicate below is not working.
class Foo2{
public:
bool operator()(const int& n) const {return n%2 == 0;}
};
template<typename Container, typename Predicate>
unsigned int my_count_if(const Container& c, Predicate p)
{
unsigned int cnt = 0;
for(const auto& x : c){
cnt += p(x) ? 1 : 0;
std::cout << "x = " << x << std::endl; // for debug
std::cout << "p(x) = " << p(x) << std::endl; // for debug
}
return cnt;
}
bool test_func(const int& n)
{
return n%2 == 0;
}
int main()
{
std::vector<int> v {1,2,3,4,5,6};
std::cout << my_count_if(v, [] (const int& n) -> bool {n%2 == 0;}); // not working
std::cout << my_count_if(v, test_func) << std::endl; // works
std::cout << my_count_if(v, Foo2()); // works
return 0;
}
The line in which the lambda is used as the predicate does not work. p(x) = 248 for all values of x, but the second version with the function object does work. Am I missing something concerning the passing of lambdas as function arguments? In this post (in the accepted answer) Pass lambda expression to lambda argument c++11 I read that
where there is no state being captured, the language allows for a conversion from the lambda type to a pointer to function with the signature of the operator() (minus the this part), so the lambda >above can be implicitly converted to a pointer
Thank you for any help!

Basic operator overloading in D (Part 2)

Using Tango with D1:
class C
{
private int j;
public int opBinary(char[] op: "+") (ref C x) { return 1; }
public int opBinary(char[] op: "+") (C x) { return 3; }
}
int opBinary(char[] op: "+") (ref C x, ref C y) { return 2; }
int opBinary(char[] op: "+") (C x, C y) { return 2; }
void main() {
C a = new C;
C b = new C;
int j = a + b;
}
Compiler error:
"incompatible types"
meaning the overloaded operators weren't matched.
Can't wait to get the hang of D.
Thanks much.
OH Yea: I'm using Tango with D1, so maybe that's why it's not working? I'd like to stick with Tango. Has anyone used Tango + D2?
In D1 templated operator overloading using opBinary, etc. doesn't work. You need to use opAdd, opSub, etc.

Resources