将函数限制为特定数据类型的正确方法

Correct way to restrict functions to specific data types

我目前在 C++ 中使用以下数据类型:

双,boost::interval<双>,和 boost 的 mpfr/mpfi 类型 (mpfr_float, mpfr_float_50, ..., mpfi_float, mpfi_float_50, ...)

我正在为这些类型中的任何一对编写一些重载函数。除了声明中的类型外,代码是相同的。

对的数量相当多,我想知道处理所有这些情况的最有效方法(使用普通形式的模板并不理想,因为应该只允许这些数字类型) .

您可以在 boost/multiprecision/mpfr.hpp 中查看 mpfr_float_... 是如何定义的。这些类型本身就是模板

typedef number<mpfr_float_backend<50> >    mpfr_float_50;
typedef number<mpfr_float_backend<100> >   mpfr_float_100;
typedef number<mpfr_float_backend<500> >   mpfr_float_500;
typedef number<mpfr_float_backend<1000> >  mpfr_float_1000;
typedef number<mpfr_float_backend<0> >     mpfr_float;

基于这一观察,我们可以很容易地得出一个只匹配这些类型的模板:

#include <iostream>

#include <boost/multiprecision/mpfr.hpp>

using boost::multiprecision::number;
using boost::multiprecision::backends::mpfr_float_backend;

template < unsigned size >
void print(number<mpfr_float_backend<size>> const &num)
{
    std::cout << "mpfr_float_" << size << ": " << num << "\n";
}


int main()
{
    using namespace boost::multiprecision;
    mpfr_float_50 a = 1;
    mpfr_float_100 b = 2;
    mpfr_float_500 c = 3;
    double d = 4;

    print(a);
    print(b);
    print(c);
  //print(d); // BOOM!
}

你当然也可以更通用一点,允许任何 Boost.Multiprecision 类型:

template < typename... T >
void print(boost::multiprecision::number<T...> const &num)
{
    std::cout << num << "\n";
}

我已经更新了答案以处理您选择的 complex/compound 类型。

处理已知的数值类型

也许你根本不关心什么库实现了这些类型:

template <typename T, typename U>
    std::enable_if_t<
        std::numeric_limits<T>::is_specialized && 
        std::numeric_limits<U>::is_specialized,
        std::common_type_t<T, U>
    > foo(T const& a, U const& b) 
{
    auto product = a * b;
    std::cout << "foo(" << a << ", " << b << ") -> " << product << "\n";
    return product;
}

请注意,这也显示了一种合成通用 return 类型的方法。

Live On Coliru

#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/mpfr.hpp>
#include <boost/multiprecision/number.hpp>

template <typename T, typename U>
    std::enable_if_t<
        std::numeric_limits<T>::is_specialized && 
        std::numeric_limits<U>::is_specialized,
        std::common_type_t<T, U>
    > foo(T const& a, U const& b) 
{
    auto product = a * b;
    std::cout << "foo(" << a << ", " << b << ") -> " << product << "\n";
    return product;
}

int main()
{
    using namespace boost::multiprecision;
    mpfr_float_50 a = 1;
    mpfr_float_100 b = 2;
    mpfr_float_500 c = 3;
    double d = 4;

    foo(a, a); foo(b, a); foo(c, a); foo(d, a); std::cout << "\n";
    foo(a, b); foo(b, b); foo(c, b); foo(d, b); std::cout << "\n";
    foo(a, c); foo(b, c); foo(c, c); foo(d, c); std::cout << "\n";
    foo(a, d); foo(b, d); foo(c, d); foo(d, d);
}

版画

foo(1, 1) -> 1
foo(2, 1) -> 2
foo(3, 1) -> 3
foo(4, 1) -> 4

foo(1, 2) -> 2
foo(2, 2) -> 4
foo(3, 2) -> 6
foo(4, 2) -> 8

foo(1, 3) -> 3
foo(2, 3) -> 6
foo(3, 3) -> 9
foo(4, 3) -> 12

foo(1, 4) -> 4
foo(2, 4) -> 8
foo(3, 4) -> 12
foo(4, 4) -> 16

Note: the weak point here is common_type because it may not know about your library types and hence fail to suggest a proper return type. See

处理复合物或化合物

在这种情况下,没有可依赖的现有类型特征。此外,这些类型是完全不相关的,因此添加单个偏特化不是问题。在这种情况下,我会使用自定义特征。

You don't describe much about the kind of operation you'll implement, so let me stick with foo. foo-ing A and B should be implemented if A and B are foo-able together.

让我们创建一个 trait 来定义哪些类型是 foo-able 的。如果 foo 是可交换的,你只需要一个 "side":

#include <type_traits>

namespace mylib { namespace traits {
    //primary template
    template <typename T, typename Enable = void> struct is_fooable : std::false_type {};

    // c++14 style
    template <typename T> constexpr bool is_fooable_v = is_fooable<T>::value;
} }

然后您可以专攻您希望支持的任何类型:

namespace mylib {
    namespace traits {
        template <typename Backend, boost::multiprecision::expression_template_option Et>
            struct is_fooable<boost::multiprecision::number<Backend, Et> >
                : std::true_type {};
        template <typename T>
            struct is_fooable<T, std::enable_if_t<std::is_arithmetic<T>{}> >
                : std::true_type {};
        template <typename T, typename Policies>
            struct is_fooable<boost::numeric::interval<T, Policies> >
                : std::true_type {};
        template <typename T>
            struct is_fooable<std::complex<T> >
                : std::true_type {};
    }

    template <typename T, typename U, typename = std::enable_if_t<traits::is_fooable_v<T> && traits::is_fooable_v<U> > >
    auto foo(T const& a, U const& b) {
        auto product = a * b;
        std::cout << "foo(" << a << ", " << b << ") -> " << product << "\n";
        return product;
    }
}

完整演示

Live On Coliru

#include <type_traits>

namespace mylib { namespace traits {
    //primary template
    template <typename T, typename Enable = void> struct is_fooable : std::false_type {};

    // c++14 style
    template <typename T> constexpr bool is_fooable_v = is_fooable<T>::value;
} }

#include <iostream>
#include <sstream>
#include <complex>
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/io.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/mpfr.hpp>
#include <boost/multiprecision/number.hpp>

namespace mylib {
    namespace traits {
        template <typename Backend, boost::multiprecision::expression_template_option Et>
            struct is_fooable<boost::multiprecision::number<Backend, Et> >
                : std::true_type {};
        template <typename T>
            struct is_fooable<T, std::enable_if_t<std::is_arithmetic<T>{}> >
                : std::true_type {};
        template <typename T, typename Policies>
            struct is_fooable<boost::numeric::interval<T, Policies> >
                : std::true_type {};
        template <typename T>
            struct is_fooable<std::complex<T> >
                : std::true_type {};
    }

    template <typename T, typename U, typename = std::enable_if_t<traits::is_fooable_v<T> && traits::is_fooable_v<U> > >
    auto foo(T const& a, U const& b) {
        std::ostringstream oss;
        oss << "foo(" << a << ", " << b << ")";
        return oss.str();
    }
}

int main()
{
    using namespace boost::multiprecision;
    mpfr_float_50 a = 1;
    float c = 3;
    std::complex<double> d(4, 1);
    boost::numeric::interval<int> b(1,1);

    using mylib::foo;
    std::cout << foo(a, a) << "; " << foo(b, a) << "; " << foo(c, a) << "; " << foo(d, a) << "\n";
    std::cout << foo(a, b) << "; " << foo(b, b) << "; " << foo(c, b) << "; " << foo(d, b) << "\n";
    std::cout << foo(a, c) << "; " << foo(b, c) << "; " << foo(c, c) << "; " << foo(d, c) << "\n";
    std::cout << foo(a, d) << "; " << foo(b, d) << "; " << foo(c, d) << "; " << foo(d, d) << "\n";
}

版画

foo(1, 1); foo([1,1], 1); foo(3, 1); foo((4,1), 1)
foo(1, [1,1]); foo([1,1], [1,1]); foo(3, [1,1]); foo((4,1), [1,1])
foo(1, 3); foo([1,1], 3); foo(3, 3); foo((4,1), 3)
foo(1, (4,1)); foo([1,1], (4,1)); foo(3, (4,1)); foo((4,1), (4,1))