H
Hill
This is just a exercise. I defined my own mem_fun1_ref_t in my_fun
space.
But got compiling error. How to fix it ? Any help is appreciated.
Administrator@HILL /m/tcplex/p3_ch18
$ cat mem_fun1_ref.cpp
#include <list>
#include <functional>
#include <iostream>
#include <algorithm>
namespace my_fun{
//class mem_fun1_ref_t
template<typename R, typename T, typename Targ> class mem_fun1_ref_t:
public std::binary_function<T, Targ, R>
{
R (T::*pmf)(Targ);
public:
explicit mem_fun1_ref_t(R (T::*p)(Targ))mf(p){}
R operator()(T& p, Targ arg){return (p.*pmf)(arg);}
};
template<typename R, typename T, typename Targ>
mem_fun1_ref_t<R, T, Targ> mem_fun_ref(R (T::*f)(Targ))
{
return mem_fun1_ref_t<R, T, Targ>(f);
}
}//end namespace my_fun
class base
{
public:
void print_with(const std::string str){
std::cout << "Just for test: " << this << str << std::endl;
}
};
int main()
{
base bn;
std::string str("TEST_STRING");
std::bind2nd(my_fun::mem_fun_ref(&base:rint_with),str)(bn);
}
Administrator@HILL /m/tcplex/p3_ch18
$ g++ mem_fun1_ref.cpp
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h: In member function `typename
_Operation::result_type std::binder2nd<_Operation>:perator()
(typename _Operation::first_argument_type&) const [with _Operation =
my_fun::mem_fun1_ref_t<void, base, std::string>]':
mem_fun1_ref.cpp:33: instantiated from here
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h:446: error: passing `const
my_fun::mem_fun1_ref_t<void, base, std::string>' as `this' argument of
`R my_fun::mem_fun1_ref_t<R, T, Targ>:perator()(T&, Targ) [with R =
void, T = base, Targ = std::string]' discards qualifiers
space.
But got compiling error. How to fix it ? Any help is appreciated.
Administrator@HILL /m/tcplex/p3_ch18
$ cat mem_fun1_ref.cpp
#include <list>
#include <functional>
#include <iostream>
#include <algorithm>
namespace my_fun{
//class mem_fun1_ref_t
template<typename R, typename T, typename Targ> class mem_fun1_ref_t:
public std::binary_function<T, Targ, R>
{
R (T::*pmf)(Targ);
public:
explicit mem_fun1_ref_t(R (T::*p)(Targ))mf(p){}
R operator()(T& p, Targ arg){return (p.*pmf)(arg);}
};
template<typename R, typename T, typename Targ>
mem_fun1_ref_t<R, T, Targ> mem_fun_ref(R (T::*f)(Targ))
{
return mem_fun1_ref_t<R, T, Targ>(f);
}
}//end namespace my_fun
class base
{
public:
void print_with(const std::string str){
std::cout << "Just for test: " << this << str << std::endl;
}
};
int main()
{
base bn;
std::string str("TEST_STRING");
std::bind2nd(my_fun::mem_fun_ref(&base:rint_with),str)(bn);
}
Administrator@HILL /m/tcplex/p3_ch18
$ g++ mem_fun1_ref.cpp
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h: In member function `typename
_Operation::result_type std::binder2nd<_Operation>:perator()
(typename _Operation::first_argument_type&) const [with _Operation =
my_fun::mem_fun1_ref_t<void, base, std::string>]':
mem_fun1_ref.cpp:33: instantiated from here
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h:446: error: passing `const
my_fun::mem_fun1_ref_t<void, base, std::string>' as `this' argument of
`R my_fun::mem_fun1_ref_t<R, T, Targ>:perator()(T&, Targ) [with R =
void, T = base, Targ = std::string]' discards qualifiers