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

  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,815
    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
    28,413
    Overloading the = operator makes sense though.
    You would probably also provide a (non-explicit) constructor that takes an int argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 11: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, 09: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, 10:07 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM