سؤال

I know that basefield is a mask for dec/oct/hex, All i want is to unset them using setf stream function but no matter how much i try it's not right, so i wrote the following example to help me understand how flags internally looks for all possible values of basefield.

#include <iostream>
using namespace std;

int main()
{
    typedef ios_base::fmtflags fmt;
    fmt a, b, c = cout.basefield;

    cout << "basefield: " << hex << c << endl;

    cout << dec;
    a = cout.flags();
    b = a & c;
    cout << "\ndec flags:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;

    cout << oct;
    a = cout.flags();
    b = a & c;
    cout << "\noct flags:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;

    cout << hex;
    a = cout.flags();
    b = a & c;
    cout << "\nhex flags:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;

    cout.setf(cout.flags() & ~cout.basefield);
    a = cout.flags();
    b = a & c;
    cout << "\nunset basefiled:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;
}

g++ 4.8.1 results are:

basefield: 4a

dec flags:
   a: 1002
   b: 2

oct flags:
   a: 1040
   b: 40

hex flags:
   a: 1008
   b: 8

unset basefiled:
   a: 1008
   b: 8

When unset all bases flags value are set to be same as hex flags, so i run the code Visual C++ 2008 and Intel C++ XE and the result was:

basefield: e00

dec flags:
   a: 201
   b: 200

oct flags:
   a: 401
   b: 400

hex flags:
   a: 801
   b: 800

unset basefiled:
   a: 801
   b: 800

Hex flags seems to be exact with all bases unset.

All i want is to set the flags to a value other than dec\oct\hex so when i implement my own manipulator i can detect which format is in use my formats or stream formats.

Thanks.

هل كانت مفيدة؟

المحلول

You can't unset flags with setf overload with single parameter - it can only set new flags. Use unsetf or setf with two parameters to unset flags.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top