模板类给出了boost类型的错误

时间:2013-09-08 08:31:45

标签: templates boost c++11 constexpr

我正在尝试实现一个template类(名为Modular),以便进行一些模块化操作,例如求和和乘法。

intint64_t工作正常,但如果我尝试传递boost整数类型,例如int128_tmpz_int,编译器会输出一些我不知道的错误非常了解。

这是类代码:

template <class T, int64_t modulus>
class Modular {
public:
    Modular() : num_(0) { }
    Modular(const T& num = 0) : num_(num % modulus) { }
    Modular(const Modular& other) : num_(other.num_) { }
    ~Modular() { }

    T get() const { return num_; }

    Modular& operator=(const Modular& other) { num_ = other.num_; return *this; }
    bool operator==(const Modular& other) { return num_ == other.num_; }
    bool operator!=(const Modular& other) { return num_ != other.num_; }
    Modular& operator++() { ++num_; num_ %= modulus; return *this; }
    Modular operator++(int) { Modular temp(*this); operator++(); return temp; }
    Modular& operator+=(const Modular& other) { num_ += other.num_; num_ %= modulus; return *this; }
    Modular& operator-=(const Modular& other) { num_ -= other.num_; num_ %= modulus; return *this; }
    Modular& operator*=(const Modular& other) { num_ *= other.num_; num_ %= modulus; return *this; }

    Modular& operator/=(const Modular& other)
    {
        if((num_ % other.num_) == 0)
            num_ /= other.num_;
        else
        {
            Modular temp(0);
            while(temp * other != num_)
                ++temp;
            num_ = temp.num_;
        }

        return *this;
    }

    Modular pow(const T& exp)
    {
        if(exp == 1)
            return *this;

        Modular square(num_ * num_ % modulus);

        if(exp & 1)
            return (*this) * square.pow(exp/2);
        else
            return square.pow(exp/2);
    }

protected:
    T num_;
};

template <class T, T modulus>
inline Modular<T, modulus> operator+(const Modular<T, modulus>& lhs, const Modular<T, modulus>& rhs)
{
    Modular<T, modulus> temp(lhs);
    temp += rhs;
    return temp;
}

template <class T, T modulus>
inline Modular<T, modulus> operator-(const Modular<T, modulus>& lhs, const Modular<T, modulus>& rhs)
{
    Modular<T, modulus> temp(lhs);
    temp -= rhs;
    return temp;
}

template <class T, T modulus>
inline Modular<T, modulus> operator*(const Modular<T, modulus>& lhs, const Modular<T, modulus>& rhs)
{
    Modular<T, modulus> temp(lhs);
    temp *= rhs;
    return temp;
}

template <class T, T modulus>
inline Modular<T, modulus> operator/(const Modular<T, modulus>& lhs, const Modular<T, modulus>& rhs)
{
    Modular<T, modulus> temp(lhs);
    temp /= rhs;
    return temp;
}

这是main()

#include <iostream>
#include <array>
#include <cstdint>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::int128_t;

int main()
{
    const int128_t n = 123;
    const int64_t mod = 456;

    typedef Modular<int128_t, mod> mint;

    const mint a = static_cast<int128_t)(789);
    const mint b = static_cast<int128_t)(12);

    mint result(0);

    for(mint x(1); x != mint(101); ++x){
        result += (a * x.pow(n+2) + b * x.pow(n+1) - x) / mint(x*x + x - mint(1));
    }
    std::cout << result.get();

    return 0;
}

编译器输出:http://pastebin.com/1fNgDedt

最初我使用const int128_t mod但是编译器说mod必须定义constexpr所以我用它替换了,但我无法理解为什么我根本无法理解第一个编译器的错误。

1 个答案:

答案 0 :(得分:3)

在您的特定平台上,boost定义int128_tboost::multiprecision::number<>,因为您的平台不提供128位内置整数类型。在C ++中,只允许使用整数数据类型作为模板参数,所以int128_t,尽管typedef的名称​​看起来像,例如int64_t,不允许作为模板的第二个参数。

相关问题