3
int var;
int *p = &var;

std::cout > var; //invalid
std::cout > p; //valid

std::cout < var; //invalid
std::cout < p; //valid

I know it seems make no meaning but what is the actually meaning of > and < in this case and why it's only valid when the right-hand operand is pointer ?

I'm using Visual Studio.

Shankar Shastri
  • 1,134
  • 11
  • 18
Van Tr
  • 5,889
  • 2
  • 20
  • 44
  • What is your compiler? – Edgar Rokjān Aug 22 '16 at 11:03
  • @EdgarRokyan I'm using Visual Studio for this. – Van Tr Aug 22 '16 at 11:04
  • Are you referring to '<' or '<<'(viceversa '>' or '>>') – Shankar Shastri Aug 22 '16 at 11:06
  • I can't reproduce this in [Clang](http://coliru.stacked-crooked.com/a/c9f0eb33f99b2750), [GCC](http://coliru.stacked-crooked.com/a/c703b36d9e887c99), or [MSVC](http://webcompiler.cloudapp.net/). Which version on Visual Studio are you using? – TartanLlama Aug 22 '16 at 11:06
  • 1
    @ShankarShastri he mean `<` and `>` obviously – phuclv Aug 22 '16 at 11:07
  • 1
    [This stream operator overload](http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool) might help explain the issue. – Some programmer dude Aug 22 '16 at 11:07
  • @TartanLlama 2010 Express – Van Tr Aug 22 '16 at 11:08
  • 1
    @TartanLlama if it is a VS compiler bug, I will remember it forever. – Van Tr Aug 22 '16 at 11:11
  • Same thing, just a different operator. – Baum mit Augen Aug 22 '16 at 11:14
  • If you use `<` with a pointer then the compiler (successfully) tries to casts the `std::out` to a pointer. It's the same as `a==1;` where a might be of any type that can be converted to `int`. Of course, both are meaningless and harmless statements. But the compiler is not able to convert the `std::cout` to the integer `var`. Posting the actual error message would be really appreciated. – harper Aug 22 '16 at 11:17
  • Seems that @JoachimPileborg has answered the question posting the appropriate link. So the `std::cout` casts implicitly to `void*` bacause of `operator void*() const` and compared with `int*` successfully. – Edgar Rokjān Aug 22 '16 at 11:23
  • It's illegal since C++11. No cast from `std::ostream` to `void *` any more. It's using explicit bool conversion now `explicit operator bool() const;` – Carousel Aug 22 '16 at 11:26
  • @Carousel Yep, but OP uses C++ 98/03. – Edgar Rokjān Aug 22 '16 at 11:29

0 Answers0