Topic: bitset question; reserved names; non-deduced contexts


Author: gp1@paradise.net.nz (Graeme Prentice)
Date: Sat, 4 Jan 2003 19:26:23 +0000 (UTC)
Raw View
Section 23.3.5 contains a (partial) class definition for the bitset
class.

Is a conforming compiler required to define this class exactly as
shown - or alternatively, to provide the functionality implied by the
definition e.g. the destructor of the inner reference class is shown
as a public user defined destructor but presumably this doesn't
require the class to actually have a user defined destructor because
this makes no difference to users of the class?

Can programmers make use of the public member proxy class called
reference that is shown.  A non const reference object is returned by
the non const operator[] member function of bitset for a non const
bitset.  A const bitset uses the const operator[] function which
returns a bool so a reference always refers to a non const bitset and
never a const bitset.

std::bitset<8> b1;
std::bitset<16> b2;
b1[0] = b2[0];
The assignment statement creates two non const reference objects and
calls the non const operator=(bool) member of the reference class to
do the assignment.

To create a reference object you can write
std::bitset<8>::reference ref1(b1[0]);

ref1 now refers to bit 0 of bitset b1.  For this to work, it is
necessary for a public constructor to be available  - none are shown
in the standard.  Can it be assumed that the implicitly defined copy
constructor reference(const reference&) is available (or the
equivalent of).

With vector<bool>, there is a member function swap that swaps two
references
static void swap(reference x, reference y);

This implies that a copy constructor is available for the reference
class but this is not shown for vector<bool> either.



<< question about reserved names >>

If I want to write a swap function for bitset, I might write in the
global namespace

template <size_t M, size_t N>
void bitswap( typename std::bitset<M>::reference x,
              typename std::bitset<N>::reference y)
{
    bool b = x;
    x = y;
    y = b;
}

I used the name bitswap partly because the lookup on a call to swap
finds only std::swap using ADL.  However, a call to bitswap fails to
compile due to a nested name specifier being a non deduced context and
the compiler refuses to deduce the M and N.

So instead, I could write in the global namespace
void bitswap( std::bitset<8>::reference x,
              std::bitset<8>::reference y)
{
    bool b = x;
    x = y;
    y = b;
}

Can the name swap be used for this function - is swap reserved by the
std library everywhere in all namespaces or is it just the signatures
of swap that the library actually uses that are reserved (in all
namespaces?)

Why is a nested name specifier a non deduced context?  The N in
bitset<N> can be deduced when it's not a nested name specifier.

Graeme

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]