Restricting a type to a specific set of values...

This is a discussion on Restricting a type to a specific set of values... within the C++ Programming forums, part of the General Programming Boards category; What I want to do is something similar to an enum. I want to declare a type that can only ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    Restricting a type to a specific set of values...

    What I want to do is something similar to an enum. I want to declare a type that can only take on the integer values from 0 to 4, inclusive. I cannot seem to find a way to do this.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What's wrong with an enum?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27
    I want to be able to use the actual integers, rather than an enumerated name.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Because he doesn't want an enum, which restrict what you can assign to a variable to a number of identifiers, but something that can be assigned to at will, just not anything other that 0 to 4. Basically, run-time bounds checking.

    OP will have to find/implement a class that wraps up a normal integer and overloads the '=' operator to catch invalid values. Or switch to Pascal, Modula-9007, Ada, or any other of their ilk.

    And it's not like an enum is any safeguard anyway:
    Code:
    #include <stdio.h>
    
    int main() {
            enum enum_t {foo, bar, baz, quux} ;
    
            enum_t x;
    
            x = foo;
    
            // an error
            x = 1;
    
            // not an error
            x = (enum_t)7;
    
            return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    The assignment "x = 1" in zx-1's example is not an error, but it does trigger a warning from most good quality compilers. Such warnings are eliminated by forcing the issue with an explicit conversion (and that, unfortunately, also allows assignment of invalid integral values to the enum).

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    27
    Hmm, I will keep that in mind. I was actually going to implement this system into a class anyhow, and include the "enum" in a declarations header file. Overloading the = operator makes sense though.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can use enum constructors to cast integers to enums and not generate any compiler warnings:
    Code:
    enum Weekday
    {
        Mon = 0,
        Tues = 1,
        Wed = 2,
        Thurs = 3,
        Fri = 4
    }
    
    Weekday day = Mon;
    day = Weekday( 2 );  // Now day == Wed.
    day = Weekday( 5 );  // Now day == 5 which isn't part of the enum (Sat maybe?)
    day = Weekday( Tues | Wed );  // Now day == 3 == Thurs.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,454
    Overloading the = operator makes sense though.
    You would probably also provide a (non-explicit) constructor that takes an int argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by grumpy View Post
    The assignment "x = 1" in zx-1's example is not an error, but it does trigger a warning from most good quality compilers. Such warnings are eliminated by forcing the issue with an explicit conversion (and that, unfortunately, also allows assignment of invalid integral values to the enum).
    It is where I'm sitting, at least:
    Code:
    $ c++ try.cc
    try.cc: In function `int main()':
    try.cc:9: error: invalid conversion from `int' to `main()::enum_t'
    It's not an error in C. Maybe that's what you're thinking of.

    ===============================
    So I see I left <stdio.h> in my example. That's a vestige of a previous try.c. So I guess it's not all your fault.
    Last edited by zx-1; 01-01-2008 at 10:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 08:51 AM
  3. Working out the mode of a set of values
    By Auz in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2002, 09:07 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21