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 8782217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:30:59+00:00 2026-06-13T20:30:59+00:00

Here is an example case of what I’m trying to do (it is a

  • 0

Here is an example case of what I’m trying to do (it is a “test” case just to illustrate the problem) :

#include <iostream>
#include <type_traits>
#include <ratio>

template<int Int, typename Type> 
constexpr Type f(const Type x)
{
    return Int*x;
}

template<class Ratio, typename Type, 
         class = typename std::enable_if<Ratio::den != 0>::type>
constexpr Type f(const Type x)
{
    return (x*Ratio::num)/Ratio::den;
}

template</*An int OR a type*/ Something, typename Type>
constexpr Type g(const Type x)
{
    return f<Something, Type>(x);
}

int main()
{
    std::cout<<f<1>(42.)<<std::endl;
    std::cout<<f<std::kilo>(42.)<<std::endl;
}

As you can see, there are two versions of the f() function : the first one takes an int as a template parameter, and the second one takes a std::ratio. The problem is the following :

I would like to “wrap” this function through g() which can take an int OR a std::ratio as first template parameter and call the good version of f().

How to do that without writing two g() functions ? In other words, what do I have to write instead of /*An int OR a type*/ ?

  • 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-06-13T20:31:00+00:00Added an answer on June 13, 2026 at 8:31 pm

    Here’s how I would do it, but I’ve changed your interface slightly:

    #include <iostream>
    #include <type_traits>
    #include <ratio>
    
    template <typename Type>
    constexpr
    Type
    f(int Int, Type x)
    {
        return Int*x;
    }
    
    template <std::intmax_t N, std::intmax_t D, typename Type>
    constexpr
    Type
    f(std::ratio<N, D> r, Type x)
    {
        // Note use of r.num and r.den instead of N and D leads to
        //   less probability of overflow.  For example if N == 8 
        //   and D == 12, then r.num == 2 and r.den == 3 because
        //   ratio reduces the fraction to lowest terms.
        return x*r.num/r.den;
    }
    
    template <class T, class U>
    constexpr
    typename std::remove_reference<U>::type
    g(T&& t, U&& u)
    {
        return f(static_cast<T&&>(t), static_cast<U&&>(u));
    }
    
    int main()
    {
        constexpr auto h = g(1, 42.);
        constexpr auto i = g(std::kilo(), 42.);
        std::cout<< h << std::endl;
        std::cout<< i << std::endl;
    }
    
    42
    42000
    

    Notes:

    1. I’ve taken advantage of constexpr to not pass compile-time constants via template parameters (that’s what constexpr is for).

    2. g is now just a perfect forwarder. However I was unable to use std::forward because it isn’t marked up with constexpr (arguably a defect in C++11). So I dropped down to use static_cast<T&&> instead. Perfect forwarding is a little bit overkill here. But it is a good idiom to be thoroughly familiar with.

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

Sidebar

Related Questions

For example (in my case) here's some code, <?php woo_post_inside_before(); if ( $woo_options['woo_post_content'] !=
Here is example which I have try <?php include 'spider/classes/simple_html_dom.php'; $html = new simple_html_dom();
Here is an example XML (case 1) : <root> <Item> <ItemID>4504216603</ItemID> <ListingDetails> <StartTime>10:00:10.000Z</StartTime> <EndTime>10:00:30.000Z</EndTime>
fiddle - here is example. I'm trying to do something looking exacly like windows
Here is an example use case of itertools.groupby() in Python: from itertools import groupby
Here is an example use case: You need to store last N (let's say
Both exist for example here: Map.empty[Int, Int] Map(1 -> 41).empty Set().empty Set.empty But here
Here an example http://jsfiddle.net/EhLsT/ $(window).scroll(function () { if ($(window).scrollTop() > $(#header).offset().top) { $(#floating).show(); }
Here an example http://jsfiddle.net/7aVXc/ Let' say current image height is 400px . How do
Here an example http://jsfiddle.net/NuzDj/ If you resize windows size, sidebar overlapping the wrapper &

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.