Thread: Calling operator. on placeholder inside Lambda expression?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    Calling operator. on placeholder inside Lambda expression?

    Hi,

    after searching the web I think something like this is not be possible because operator. can't be overloaded in c++:

    Code:
    #include <map>
    #include <ostream>
    #include <algorithm>
    #include <boost/lambda/lambda.hpp>
    
    typedef std::map<int, dem::EntityViewer> AgentDatabase;
    
    std::ostream&
    operator<<(std::ostream& os, const AgentDatabase& adb)
    {
    	std::for_each(adb.begin(), adb.end(), os << boost::lambda::_1.first << " ");
    	return os;
    }
    But maybe someone knows a nice workaround preventing me from writing a custom fuctor, which would force me to scatter code which belongs together.

    Thank you in advance!

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I got it! I'had to use lambda::bind.

    Code:
    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    
    using boost::lambda::bind;
    using boost::lambda::_1;
    using boost::lambda::constant;
    
    typedef std::map<int, std::string> MapType;
    
    std::ostream&
    operator<<(std::ostream& os, const MapType& map)
    {
    	std::for_each(map.begin(), map.end(), os
    		<< constant("key: ")
    	        << bind(&MapType::value_type::first, _1)
    		<< " value: " << bind(&MapType::value_type::second, _1)
    		<< "\n"
    //		<< std::endl
    		);
    	return os;
    }
    
    int main(int argc, char** argv)
    {	
    	MapType myMap;
    	myMap[0] = "nothing";
    	myMap[1] = "at least a bit";
    	myMap[500] = "a lot";
    	
    	std::cout << myMap << std::endl;
    }
    But can anybody tell me, why the compile fails, if I comment in the blue line?
    The error with g++ is:

    **** Build of configuration Debug for project ttest ****

    make all
    Building file: ../main.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
    ../main.cpp: In function ‘std::ostream& operator<<(std::ostream&, const MapType&)’:
    ../main.cpp:28: error: no match for ‘operator<<’ in ‘boost::lambda::operator<<(const boost::lambda::lambda_functor<T>&, const B&) [with Arg = boost::lambda::lambda_functor_base<boost::lambda:: bitwise_action<boost::lambda::leftshift_action>, boost::tuples::tuple<boost::lambda::lambda_functor <boost::lambda::lambda_functor_base<boost::lambda: :bitwise_action<boost::lambda::leftshift_action>, boost::tuples::tuple<boost::lambda::lambda_functor <boost::lambda::lambda_functor_base<boost::lambda: :bitwise_action<boost::lambda::leftshift_action>, boost::tuples::tuple<boost::lambda::lambda_functor <boost::lambda::lambda_functor_base<boost::lambda: :bitwise_action<boost::lambda::leftshift_action>, boost::tuples::tuple<std::basic_ostream<char, std::char_traits<char> >&, boost::lambda::lambda_functor<boost::lambda::ident ity<const char (&)[6]> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::lambda::lambda_functor<boost::lambda::lambd a_functor_base<boost::lambda::action<2, boost::lambda::function_action<2, boost::lambda::detail::unspecified> >, boost::tuples::tuple<const int std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::* const, const boost::lambda::lambda_functor<boost::lambda::place holder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, const char (&)[9], boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::lambda::lambda_functor<boost::lambda::lambd a_functor_base<boost::lambda::action<2, boost::lambda::function_action<2, boost::lambda::detail::unspecified> >, boost::tuples::tuple<std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::* const, const boost::lambda::lambda_functor<boost::lambda::place holder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> >, B = char [2]](((const char (&)[2])"\012")) << std::endl’
    ../main.cpp:21: note: candidates are: std::ostream& operator<<(std::ostream&, const MapType&)
    make: *** [main.o] Error 1

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pheres View Post
    But can anybody tell me, why the compile fails, if I comment in the blue line?
    The error with g++ is:
    Don't you love compiler errors when there's complicated metaprogramming going on?

    I've run into this before. I think it's a bug in boost::lambda. Just use "\n" instead.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by brewbuck View Post
    Don't you love compiler errors when there's complicated metaprogramming going on?
    It makes for a nice office game: The one who takes longest to read it loud without errors has to fix it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  2. Calling other objects methods inside a constructor
    By mynickmynick in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2008, 06:31 AM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 01:22 AM
  5. calling a member function inside a vector
    By finnepower in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2004, 02:44 AM