Home > OS >  How can I static cast the output of the >> operator in c ?
How can I static cast the output of the >> operator in c ?

Time:02-05

Or perform any function really.

Currently I have some code that looks a bit like this:

int temp_int;
streamer >> temp_int;
finalObj = static_cast<CustomType>(temp_int);

ints can be cast into CustomTypes (obviously) and finalObj is of type CustomType

streamer's >> operator has not been set up to work with CustomType. Changing this is outside my area of control.

Im looking for a way to put the above code in a single line and avoid using a temporary variable.

When putting custom types back into the streamer, I use this:

streamer << static_cast<int>(someObj);

Thats a lot neater. And it would be great to have the instreaming and outstreaming look symmetrical for easier reading.

Thanks

CodePudding user response:

Just implement an operator>> and make sure name lookup finds it.

struct CustomType
{
    int m_value;
    CustomType(int v = 0)
        : m_value(v)
    {}

    operator int() const
    {
        return m_value;
    }
};
namespace
{
    std::istream& operator>>(std::istream& s, CustomType& out)
    {
        int temp;
        s >> temp;
        out = temp;
        return s;
    }
}

int main()
{
    CustomType finalObj;
    std::cin >> finalObj;
    std::cout << finalObj << '\n';
}

Alternatively, if you aren't sure, if operator>> has already been implemented somewhere in an undesired manner, you could create a wrapper type and implement operator>> for it.

namespace
{
    class CustomTypeParsingWrapper
    {
    public:
        CustomTypeParsingWrapper(CustomType& target)
            : m_target(target)
        {
        }

        friend std::istream& operator>>(std::istream& s, CustomTypeParsingWrapper const& wrapper)
        {
            int temp;
            s >> temp;
            wrapper.m_target = temp;
            return s;
        }

    private:
        CustomType& m_target;
    };
}

int main()
{
    CustomType finalObj;
    std::cin >> CustomTypeParsingWrapper(finalObj);
    std::cout << finalObj << '\n';
}

Note: The use of the anonymous namespace here is only to avoid conflicts with implementations provided to other translation units. If you want to reuse operator>>, you should put it in the same namespace as CustomType to allow Argument Dependent Lookup to find it. A customTypeParsingWrapper intended to be reused could be put in a namespace of your choice...

CodePudding user response:

One way to achieve what you want is to overload not only << and >> operators, but also the cast operator.

One way to do that is to overload the int() operator for CustomType. This way, you can use put your value back into the streamer easily:

class CustomType
{
private:
    int _value;
public:
    CustomType(const int &value) : _value(value) {}

    // ...
    operator int() const { return _value; }

};

This way, the compiler will find the appropriate cast from CustomType to int when using the << operator:

streamer << someObj;

will implicitly cast to int.

Similar overloads could be implemented to achieve the results.

Here is a full snipped to test this:

#include <iostream>

class CustomType
{
private:
    int _value;
public:
    CustomType(const int &value) : _value(value) {}

    // ...
    operator int() const { return _value; }

};

int main() {

  auto someObj = CustomType(10);
  std::cout << someObj;
}

This prints 10 to the standard output.

  •  Tags:  
  • Related