Topic: On strong types, literal values and such things


Author: magfr@comp.std.cpp.magfr.user.lysator.liu.se (Magnus Fromreide)
Date: Mon, 9 Feb 2004 00:10:12 +0000 (UTC)
Raw View
Hello

I have read the proposals for strong typedefs as well as the one about
literal values and likes both but would like to se a little twist as
I think that the syntax with using is somewhat peculiar.

Why not use the only keyword that already adds a new type that is
incompatible with it's underlying type, that is enum.

With that we could get

typedef Classtype enum { } Newtype;

but since enums already have syntax of their own that would be equal to

Classtype enum Newtype { };

This also addresses the problem of how to add user supplied names to
constant values as follows:

Classtype enum {
        ClasstypedConstant = Classtype(4711)
};

Note that ClasstypedConstant is of an anonymous type but since enum values
lower to their base types it is assignable to a Classtype as well.

I do feel that it would be good if only literal values were allowed as right
hand sides in this kind of expression, especially if the proposal for widening
of the concept literal values is passed.

What would be the meaning of

Classtype enum { x };

One could argue that it should be a default constructed Classtype but then
the question is what

Classtype enum { x, y };

should give as value to y, x + 1? But what if Classtype lacks operator+
or even if it just isn't pure enough to give a literal value. I think
it would be best to avoid this can of worms by not defining the result
when the initializer is omitted.

This means that

int enum { x };

is undefined while

enum { x };

is well defined.

---
[ 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                       ]