CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    find a type based on value ?

    Another fun question...
    Is it possible to make a type, based on a value ?

    What I'm looking for is way to return the smallest (sizeof) unsigned integral type that will hold a certain value.

    smallest_type<14>::type // unsigned char
    smallest_type<127>::type // unsigned char
    smallest_type<255>::type // unsigned char
    smallest_type<256>::type // unsigned short
    smallest_type<64000>::type // unsigned short
    smallest_type<128000>::type // unsigned long
    smallest_type<5000000000>::type // unsigned long long

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: find a type based on value ?

    Why wouldn't it?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: find a type based on value ?

    Take a look at this

  4. #4
    Join Date
    Oct 2008
    Posts
    1,456

    Re: find a type based on value ?

    you can use the new cstdint to simplify this. Note that boost already did it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured