|
Boost Users : |
Subject: [Boost-users] Bind/Function: use reference arguments in template variadic functions
From: Yannick POTIN (yannp63_at_[hidden])
Date: 2013-04-11 16:57:09
Hello,
I'm working on a little project which use the template variadic technology.
I use them with boost::bind and boost::function and found a very annoying bug.
Because I have a lot of files very long and (sorry :-( ) uncommented.
I made a simple but explicit example like this one :
----------------------------------------------
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
// A classic class...
class A {
public:
// ... with a constructor
A(int a) : a(a) { }
// ... an operator
void operator()() {
std::cout << (void*)this << "::" << __func__ << "(): " << a << std::endl;
}
// ... and a member
int a;
};
// A stupid function...
void call_a(A &a) {
// ... which do something usefull
// please not that this function NEEDs a reference
a();
}
// A template variadic function...
template<class ...classes>
boost::function0<void> call_something(classes... c) {
// ... here we are:
return boost::bind(call_a,boost::ref(c...));
}
// Ok, this one, I think you know it more than I do
int main(void) {
// Let's create some objets
A a1(1);
A a2(2);
// Call them to print the memory
// position (see A::operator()())
a1();
a2();
// Create a boost::function0<void> and call it
call_something<A>(boost::ref(a1))();
// Again
call_something<A>(boost::ref(a2))();
return 0;
}
----------------------------------------------
After compilation, I get this output (watch the memory addresses):
0xbfc8f994::operator()(): 1
0xbfc8f990::operator()(): 2
0xbfc8f984::operator()(): 1
0xbfc8f984::operator()(): 2
I really don't know what happened (even if I can imagine).
But references are just lost and this is not good.
So, here is my question: How can I make this working ?
May I precise that this example only use one argument but my project
actually use at most five so
the boost::ref() call like in the call_something function is impossible
(and does not work anyway).
Before someone ask me if I can use pointer instead, I answer:
Yes I can, but it will take a lot of time
and if I can avoid this, I will.
Thank you for reading, for your patience and for your answers ;-)
Regards,
Yannick POTIN
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net