Passing an integer or a type as a template parameter?

做~自己de王妃 提交于 2021-02-19 03:10:50

问题


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:


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.




回答2:


How to do that without writing two g() functions ?

You don't. There is no way in C++ to take either a type or a value of some type, except through overloading.




回答3:


It is not possible to have a template parameter taking both type and non-type values.

Solution 1:

Overloaded functions.

Solution 2:

You can store values in types. Ex:

template<int n>
struct store_int
{
    static const int num = n;
    static const int den = 1;
};

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<typename Something, typename Type>
constexpr Type g(const Type x)
{
    return f<Something, Type>(x);
}

But with this solution you will have to specify g<store_int<42> >(...) instead of g<42>(...)

If the function is small, I advise you to use overloading.



来源:https://stackoverflow.com/questions/13220920/passing-an-integer-or-a-type-as-a-template-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!