Thread overview
operator overload
Dec 12
dark777
Dec 12
Biotronic
Dec 12
Biotronic
Dec 12
dark777
Dec 12
dark777
December 12
I know that this community is not of c ++, but some time I have been studying how to do overload of ostream operators in c ++ and I even managed to get to this result but the same is not converting to binary only presents zeros as output to any number already tried to pass parameter of variable and yet he is not getting the number for conversion how to solve it so that it works correctly?

PS: if I use the same conversion algorithm in a function it converts normally it is not only converting to the output of the operator ..


https://pastebin.com/BXGXiiRk
December 12
On Tuesday, 12 December 2017 at 15:52:09 UTC, dark777 wrote:
> I know that this community is not of c ++, but some time I have been studying how to do overload of ostream operators in c ++ and I even managed to get to this result but the same is not converting to binary only presents zeros as output to any number already tried to pass parameter of variable and yet he is not getting the number for conversion how to solve it so that it works correctly?
>
> PS: if I use the same conversion algorithm in a function it converts normally it is not only converting to the output of the operator ..
>
>
> https://pastebin.com/BXGXiiRk

This line:
    << "\n\t127 em binario:     " << bin << 127 << "\n\n";

Your hope is that the '<< bin' part should behave just like std::hex does, except binary instead of hex. Right?

I'm afraid that's not how C++ formatting works. You can see a hint of what happens in the output you get:
    127 em binario:     000000000000127

The zeroes are from the uninitialized int in your Bin struct, and the '127' is from the 127 you pass right after passing bin.

Translating to code that does not use operators, the behavior you expect is something like this:

    std::cout.print("\n\t127 em binario:     ");
    std::cout.setFormat(bin);
    std::cout.print(127); // Should use bin for formatting
    std::cout.print("\n\n");

The actual behavior is something like this:

    std ::cout.print("\n\t127 em binario:     ");
    std::cout.print(bin); // Prints the value stored in bin. That is, 0.
    std::cout.print(127); // Prints 127 just the way it would otherwise print it.
    std::cout.print("\n\n");

There is no way in C++ to set the format the way you want it. If you want binary output, you need to call a function like your binario function.

The point of overloading operator<<(std::ostream&, MyType) is not to format another type, but to be able to print MyType in a given way. Basically like toString() in D, C# or Java.

--
   Biotronic
December 12
On Tuesday, 12 December 2017 at 16:54:17 UTC, Biotronic wrote:
> There is no way in C++ to set the format the way you want it. If you want binary output, you need to call a function like your binario function.

Of course this is not entirely true - there is a way, but it's ugly and probably not what you want:

    struct BinStream
    {
        std::ostream& os;
        BinStream(std::ostream& os) : os(os) {}

        template<class T>
        BinStream& operator<<(T&& value)
        {
            os << value;
            return *this;
        }

        BinStream& operator<<(int value)
        {
            os << binario(value);
            return *this;
        }

        std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&))
        {
            return os << _Pfn;
        }
    };

    struct Bin
    {
        friend BinStream operator<<(std::ostream& os, const Bin& f);
    } bin;

    BinStream operator<<(std::ostream& os, const Bin& f)
    {
        return BinStream(os);
    }

int main()
{
    std::cout << "\n\t127 em binario:     " << binario(127)
        << "\n\t127 em binario:     " << bin << 127
        << "\n\t127 em octal:       " << std::oct << 127
        << "\n\t127 em binario:     " << bin << 127
        << "\n\t127 em hexadecimal: " << std::hex << 127
        << "\n\t127 em binario:     " << bin << 127
        << "\n\t127 em decimal:     " << std::dec << 127
        << "\n\t127 em binario:     " << bin << 127 << "\n\n";
}

What is this black magic? Instead of overriding how std::ostream does formatting, Bin::Operator<< now returns a wrapper around a std::ostream, which special cases ints. If it gets any other format specifiers, it returns the ostream again, and the binary formatting is gone.

All in all, I think the conclusion is: Stay with D.

--
  Biotronic
December 12
On Tuesday, 12 December 2017 at 17:13:55 UTC, Biotronic wrote:
> On Tuesday, 12 December 2017 at 16:54:17 UTC, Biotronic wrote:
>> There is no way in C++ to set the format the way you want it. If you want binary output, you need to call a function like your binario function.
>
> Of course this is not entirely true - there is a way, but it's ugly and probably not what you want:
>
>     struct BinStream
>     {
>         std::ostream& os;
>         BinStream(std::ostream& os) : os(os) {}
>
>         template<class T>
>         BinStream& operator<<(T&& value)
>         {
>             os << value;
>             return *this;
>         }
>
>         BinStream& operator<<(int value)
>         {
>             os << binario(value);
>             return *this;
>         }
>
>         std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&))
>         {
>             return os << _Pfn;
>         }
>     };
>
>     struct Bin
>     {
>         friend BinStream operator<<(std::ostream& os, const Bin& f);
>     } bin;
>
>     BinStream operator<<(std::ostream& os, const Bin& f)
>     {
>         return BinStream(os);
>     }
>
> int main()
> {
>     std::cout << "\n\t127 em binario:     " << binario(127)
>         << "\n\t127 em binario:     " << bin << 127
>         << "\n\t127 em octal:       " << std::oct << 127
>         << "\n\t127 em binario:     " << bin << 127
>         << "\n\t127 em hexadecimal: " << std::hex << 127
>         << "\n\t127 em binario:     " << bin << 127
>         << "\n\t127 em decimal:     " << std::dec << 127
>         << "\n\t127 em binario:     " << bin << 127 << "\n\n";
> }
>
> What is this black magic? Instead of overriding how std::ostream does formatting, Bin::Operator<< now returns a wrapper around a std::ostream, which special cases ints. If it gets any other format specifiers, it returns the ostream again, and the binary formatting is gone.
>
> All in all, I think the conclusion is: Stay with D.
>
> --
>   Biotronic

I understand is basically this now I will see the necessity of this basically the example I wanted to do is to understand how it would be done in the most correct way, now I will study to do other cases if nescessarios but primarily is to convert only integers but you did exactly the that I wanted

I did some studies in the D language and it already has a way to convert to binary without any work just using% b as in printf you use% X or% x to convert to exa ... and that was the reason that made me interested in understand this was looking at some older glibc and the way printf understands these very interesting conversion operators
December 12
On Tuesday, 12 December 2017 at 17:13:55 UTC, Biotronic wrote:
> On Tuesday, 12 December 2017 at 16:54:17 UTC, Biotronic wrote:
>> There is no way in C++ to set the format the way you want it. If you want binary output, you need to call a function like your binario function.
>
> Of course this is not entirely true - there is a way, but it's ugly and probably not what you want:
>
>     struct BinStream
>     {
>         std::ostream& os;
>         BinStream(std::ostream& os) : os(os) {}
>
>         template<class T>
>         BinStream& operator<<(T&& value)
>         {
>             os << value;
>             return *this;
>         }
>
>         BinStream& operator<<(int value)
>         {
>             os << binario(value);
>             return *this;
>         }
>
>         std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&))
>         {
>             return os << _Pfn;
>         }
>     };
>
>     struct Bin
>     {
>         friend BinStream operator<<(std::ostream& os, const Bin& f);
>     } bin;
>
>     BinStream operator<<(std::ostream& os, const Bin& f)
>     {
>         return BinStream(os);
>     }
>
> int main()
> {
>     std::cout << "\n\t127 em binario:     " << binario(127)
>         << "\n\t127 em binario:     " << bin << 127
>         << "\n\t127 em octal:       " << std::oct << 127
>         << "\n\t127 em binario:     " << bin << 127
>         << "\n\t127 em hexadecimal: " << std::hex << 127
>         << "\n\t127 em binario:     " << bin << 127
>         << "\n\t127 em decimal:     " << std::dec << 127
>         << "\n\t127 em binario:     " << bin << 127 << "\n\n";
> }
>
> What is this black magic? Instead of overriding how std::ostream does formatting, Bin::Operator<< now returns a wrapper around a std::ostream, which special cases ints. If it gets any other format specifiers, it returns the ostream again, and the binary formatting is gone.
>
> All in all, I think the conclusion is: Stay with D.
>
> --
>   Biotronic

I tried to execute with this part of the code

  std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&))
  {
   return os <<_Pfn;
  }

and it returns me this error below what is the include?

bin2.cxx:44:43: error: expected ‘,’ or ‘...’ before ‘(’ token
   std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&))
                                           ^
bin2.cxx: In member function ‘std::ostream& BinStream::operator<<(std::ios_base&)’:
bin2.cxx:46:16: error: ‘_Pfn’ was not declared in this scope
    return os <<_Pfn;