Type Promotion in function call

This is a discussion on Type Promotion in function call within the C++ Programming forums, part of the General Programming Boards category; I have not compiled this just looking for advice: Code: unsigned functionFoo() { unsigned fooNum = 0; //... return fooNum; ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409

    Type Promotion in function call

    I have not compiled this just looking for advice:

    Code:
    unsigned functionFoo()
    {
    	unsigned fooNum = 0;
    	//...
    	
    	return fooNum;
    }
    
    void functionBar(unsigned long myNum)
    {
    	//..
    }
    
    int main()
    {
    	unsigned num = functionFoo();
    	functionBar(num);
    	
    	return 0;
    }
    So is this safe? Its is for a random number generator, so i would like to be sure everything is tickety-boo, it's using Prelude's mersenne twister implementation, the seed function takes the unsigned long int, but prior to that i am using a seperate time_t hash function that returns the unsigned int
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's safe in that conversion from unsigned int -> unsigned long is guaranteed to be lossless. It may be unsafe in that it may make the outcome of the rng more predictable than necessary (if long is actually larger than int, you're not using the full range of the seed), but that's unlikely to be a serious problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM

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