Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6645793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:20:01+00:00 2026-05-26T00:20:01+00:00

I wish to write a template structure foo such that foo<N>::value_type is the nearest

  • 0

I wish to write a template structure foo such that foo<N>::value_type is the nearest sized integer (rounding up) to N. For example foo<32>::value_type => uint32_t, foo<33>::value_type => uint64_t and foo<72>::value_type => uint64_t.

To do this I need an elegant means of providing partial specializations of foo for a range of values, e.g, 1 <= N <= 8 to return uint8_t and so on and so fourth. Is there a means of accomplishing this without having to specialise everything from 0 to 64.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:20:02+00:00Added an answer on May 26, 2026 at 12:20 am
    template<size_t N> struct select { typedef uint64_t result; };
    template<> struct select<0> { typedef uint8_t result; };
    template<> struct select<1> { typedef uint16_t result; };
    template<> struct select<2> { typedef uint32_t result; };
    
    template<size_t N>
    struct foo
    {
        enum{D = (N > 32 ? 3 : (N > 16 ? 2 : (N > 8 ? 1 : 0)))};
    
        typedef typename select<D>::result value_type;
    
        value_type value;
    };
    

    In c++11 you can use std::conditional:

    typedef 
        typename std::conditional<(N > 32), uint64_t,
        typename std::conditional<(N > 16), uint32_t,
        typename std::conditional<(N > 8), uint16_t, uint8_t>
        ::type>::type>::type value_type;
    

    You can decide which one is less readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to write a simple javascript function that toggles the visibility of a
I am using Qt and wish to write a class that will perform some
I wish to write an API that can get the contacts list from Gmail,
I wish to write a small tool that will capture a global event when
I wish to write a structure made up of fixed length strings to a
I have a component that I wish to write and it's the kind of
I wish to write a function that operates on any value that can be
I wish to write a C# WinForms application that can play a WAV file.
I wish to write a simple git script that will run the following lines:
I wish to write a shell script that calls a sql procedure(ultimately run the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.