Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Is std::cout << "test"; true?

Name: noob 2008-07-12 20:39

Is std::cout << "test"; true?

Name: Anonymous 2008-07-12 20:41

bump

Name: Anonymous 2008-07-12 20:41

YES

Name: Anonymous 2008-07-12 20:44

It doesn't return bool but it is true unless output failed!

Name: Anonymous 2008-07-13 0:01

>>4
Hahaha, what? That would be pretty logical. We're talking about sepples here. The results is always iostream & or some shit, so yeah, it will be true.

Name: Anonymous 2008-07-13 7:04

>>5
Since it's a reference to an object it cannot be NULL can it?

Name: Anonymous 2008-07-13 7:29

>>6
A stream object derived from ios can be casted to a pointer. This pointer is a null pointer if either one of the error flags (failbit or badbit) is set, otherwise it is a non-zero pointer.

The pointer returned is not intended to be referenced, it just indicates success when none of the error flags are set.

Name: Anonymous 2008-07-13 10:57

>>5
iostream::operator<< returns an iostream&, yes. However, iostream defines a cast to void*1. So, this can be used in a conditional part of an if expression:
while(cin >> x)
  cout << x + 1;


Of course, operator void* isn't a very safe construct. But operator bool isn't so great either: it still allows you to say cout == cin and get true back, or pass it to a function expecting a boolean parameter. It's best to do something like this (where T is the name of the class implementing this pattern):

    typedef bool(T::* unspecified_bool_type)();
    operator unspecified_bool_type() const {
        return convert_to_bool() ? &T::convert_to_bool : unspecified_bool_type();
    }

    bool convert_to_bool() const {
        return !!(rand()%2);
    }


I wrapped it like this, using the CRTP2:

template<class Derived>
struct testable {
    typedef bool(Derived::* unspecified_bool_type)();

    operator unspecified_bool_type() const {
        return static_cast<Derived*>(this)->convert_to_bool() ? &Derived::convert_to_bool : unspecified_bool_type();
    }

protected:
    //Not const, thus, causes an error.
    void _error_cannot_compare_testables_defaultly() {}

    /**
     * \remarks Don't allow comparisons.
     * Declared as a friend function - it will be found due to argument dependent lookup,
     * similar to friend name injection.
     * This function is obviously invalid, and can never compile, but the compiler won't
     * try to instantiate it until someone actually tries to compare.
     */
    template<class T>
    friend bool operator == (const Derived& left, const T& right) {
        left._error_cannot_compare_testables_defaultly(); return false;
    }

    template<class T>
    friend bool operator != (const Derived& left, const T& right) {
        left._error_cannot_compare_testables_defaultly(); return false;
    }
};

_________________________________
1http://www.cplusplus.com/reference/iostream/ios/operator_voidpt.html
2See http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern

Name: Anonymous 2008-07-13 12:08

>>8
WHBT. For 25 years. By Sepples.

Name: Anonymous 2008-07-13 13:02

>>8
fail, that code doesn't work.

Name: Anonymous 2008-07-13 14:09

>>10
Elaborate. I suspect you don't understand Sepples, for I have used this code and it worked.

Name: Anonymous 2008-07-13 14:42

how do you pronounce cout?
is it like coot?
or coat? ...

Name: Anonymous 2008-07-13 15:19

>>12
I pronounce cout as see-out.

Name: Anonymous 2008-07-13 15:31

>>12
Kuh - yew - t

Name: Anonymous 2008-07-13 15:36

>>14

Cool jews?

Name: Anonymous 2008-07-13 19:36

>>12
Console output

Name: Anonymous 2008-07-13 20:06

>>15
- Henry Winkler
- ??????

Name: Anonymous 2008-07-14 6:47

>>16
Actually, the c stands for character and not console.

Name: Anonymous 2008-07-14 9:23

>>18
no it doesn't, the Sussman said so

Name: Anonymous 2008-07-14 9:43

>>19
Fallacious appeal to authority: detected.

Name: Anonymous 2008-07-14 10:15

>>20
An appeal to the Sussman is never fallacious.

Name: Anonymous 2008-07-14 10:32

>>21
But it is fellatious.

Name: Anonymous 2008-07-15 4:33

_error_cannot_compare_testables_defaultly
I lol'd

Name: Anonymous 2009-03-06 7:52

And the size of   archive I think   if I were   back to the   current project manner   It is very   possible to get   it in the   present and easy   task but nothing   it simply downloads   the page so   what am I   just pskill bash   and everything solved   oh and the   impression I got.

Name: ​​​​​​​​​​ 2010-10-23 23:19

Name: Anonymous 2011-02-03 6:05

Name: Anonymous 2011-02-04 19:09


Don't change these.
Name: Email:
Entire Thread Thread List