[proto] Object got sliced when passed by reference

Hi all I have a class tree as the following template<typename T> struct base{ bool operator()(int i)const { return static_cast< const T&>(*this)(i); } }; struct F1:public base<F1>{ bool operator()(int i) const{ return true; } }; struct F2:public base<F2>{ bool operator()(int i) const{ return true; } }; struct F3 :public base<F3>{ std::string junk_; F3(std::string junk):junk_(junk){} bool operator()(int i) const{ std::cout << junk_ << std::endl; return false; } }; ================================================================== I am trying to use proto to derive the following systax Predicate<F1> f1; Predicate<F2> f2; Predicate<F3> f3("hodqhoqw"); cout << ((f1||f2)&&f3)(1) << endl; ================================================================== When proto get to the point of invoking the F3::operator(), the junk_ attribute got sliced away. I thought I am passing everything by constant reference but the exception tells me otherwise. The full sample code is attached, can someone tell me what I have missed? I am testing my code on vc9 and vc10, and I am not using virtual function for performance issue. Ultimately, I want to have the following syntax in the client code F1 f1; F2 f2; F3 f3("hodqhoqw"); cout << ((f1||f2)&&f3)(1) << endl; Thanks

I just compiled your smaple using g++-4.5 and i got: logical_and( logical_or( terminal(4baseI2F1E) , terminal(4baseI2F2E) ) , terminal(4baseI2F3E) ) hodqhoqw 0 Why is I think the correct answer. So this is tied to some MSVC specific behavior, i guess mayeb on how the terminal< base<_> const &> is matched. matching terminal< base<_> > should be enough I think no ? Could you try ? HTH
participants (2)
-
Joel Falcou
-
Leon Sit