Thread: Binding cout?

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Binding cout?

    Just a question if someone has done it.
    I was wondering if (or rather how) I can bind std::cout with boost::bind.
    The idea I'm playing around with is to bind an error message to std::cout to reduce code.
    I just can't get the syntax right.

    Code:
    boost::bind(&std::operator << < std::char_traits<char> >, std::cout, "Test");
    This is "closest" I've come to bind it.
    Any ideas (still looking for the proper return type, too)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Binding overloaded operators is ... tricky, to say the least.

    Basically, you need to do an explicit cast in order to let the compiler select the correct overload. That is, you need something like
    Code:
    boost::bind(
      static_cast< std::ostream& (*)(std::ostream&, const char*) >(&std::operator << <std::char_traits<char> >), std::cout, "Test");
    The whole thing is such a mess that you don't really save any code. Just thing about how often
    std::cout << msg;
    fits into the horrible line above.

    A better choice here would be Boost.Lambda:
    (ll::ref(std::cout) << "Test")
    Assuming you have
    namespace ll = boost::lambda
    in effect.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can see it will be a pain...
    I can't seem to find the doc for boost::lambda::ref, though.
    Boost's site is so difficult to find things on.

    But I'll have to try that and scrap the old idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Could be boost::ref. It's a little tool that is used in more than one place, and I don't know where its documentation is.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I found this link. But that isn't for boost::lambda::ref. It is for boost::ref.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I was looking for this, actually:
    http://www.boost.org/doc/libs/1_36_0..._and_variables
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Nasty... Is that really going to benefit your program, Elysia?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not nasty. It's very powerful and very flexible.
    I wonder if other programming languages has anything close to this.
    This is a big example where C++ really shines.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Did I say anything inflamatory about what you are doing? Or rather did I simply make a statement as to how your code will end up looking?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know what you meant. What did you mean, because I'm having problems seeing a any problems.
    Unless you're talking about bind, which is nasty.
    Lambda is way better. Not nasty at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Better and certainly not as ugly as bind. But not necessarily visually appealing either.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I see no problems with it what-so-ever.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Duly noted.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Just a question if someone has done it.
    I was wondering if (or rather how) I can bind std::cout with boost::bind.
    The idea I'm playing around with is to bind an error message to std::cout to reduce code.
    I just can't get the syntax right.

    Code:
    boost::bind(&std::operator << < std::char_traits<char> >, std::cout, "Test");
    This is "closest" I've come to bind it.
    Any ideas (still looking for the proper return type, too)?
    Indirection might help.

    Code:
    template <typename T>
    std::ostream &output(std::ostream &stream, const T &val)
    {
        return stream << val;
    }
    Now this output<T>() is something that is easier to bind. A better name for it is probably in order, though.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I will take that into account. Thanks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 07:27 PM
  2. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 11:06 PM
  3. Homework due tonight! help please!!
    By Andy717 in forum C++ Programming
    Replies: 41
    Last Post: 04-07-2005, 01:18 PM
  4. How Does cout Work
    By jrahhali in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2004, 03:19 PM
  5. FAQ cout
    By evilmonkey in forum FAQ Board
    Replies: 1
    Last Post: 10-07-2001, 11:32 AM