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

  • SEARCH
  • Home
  • 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 6843937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:18:51+00:00 2026-05-27T00:18:51+00:00

Assume I have a function like this: static const boost::int32_t SOME_CONST_VALUE = 1073741823; template<typename

  • 0

Assume I have a function like this:

static const boost::int32_t SOME_CONST_VALUE = 1073741823;
template<typename targetType, typename sourceType>
targetType Convert(sourceType source)
{
    typedef decltype(source * SOME_CONST_VALUE) MulType_t;
    //typedef boost::int64_t MulType_t;
    MulType_t val = (MulType_t)source * (MulType_t)SOME_CONST_VALUE;
    return val / (MulType_t)SOME_CONST_VALUE;
}

When I call this function like this

boost::int32_t i = std::numeric_limits<boost::int32_t>::max();
boost::int32_t k = Convert<boost::int32_t>(i);

k equals 1, because of the overflow during the multiplication. Casting everything to boost::int64_t will lead to the result I want. But I don’t want to cast a short or char to a int64 value.
So can I use the decltype to get the next larger type of the expression.

  • 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-27T00:18:52+00:00Added an answer on May 27, 2026 at 12:18 am

    You have to make your own specialization of a template for that:

    template<typename tp>
    class bigger { }
    
    template
    class bigger<boost::int8_t>
    {
        typedef boost::int16_t type;
    }
    
    template
    class bigger<boost::int16_t>
    {
        typedef boost::int32_t type;
    }
    
    template
    class bigger<boost::int32_t>
    {
        typedef boost::int64_t type;
    }
    

    You can also make a macro if you don’t like typing alot:

    #define BIGGER(x, y) \
        template \
        class bigger<boost::int##x##_t> \
        { \
            typedef boost::int##y##_t type; \
        }
    
    BIGGER(8, 16);
    BIGGER(16, 32);
    BIGGER(32, 64);
    

    and then use it like

    bigger<boost::int32_t>::type x;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume I have a number with 7 digits. I need a function like this
Let's assume we have a template function foo: template<class T> void foo(T arg) {
I have a question regarding static function in php. let's assume that I have
I have an object called ValueBox that I created like this: function ValueBox(params) {
So I have a function, written in C++, that looks like this... extern C
Lets assume we have a class car. How would You name parameters of function
In C++, I have a function: void MyFunction(int p) { p=5; } Assume, I
Assume I have this XML... <books> <book> <author> <title> <publish_date> <isbn_number> <book> </books> ...how
Suppose i have pointer to a thread like this CWinThread *m_pThread = AfxBeginThread(StartThread, this,

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.