c++ - 结合 GMPXX 和 C++11 及更高版本

标签 c++ c++11 gmp auto

我在结合 GMP 和 C++11 时遇到了小问题。

示例程序:

#include <gmpxx.h>

int main()
{
    mpz_class a,b; //ok
    auto c = a+b; //ok (?)
    c = 0; //error
}

错误信息:

error: no match for 'operator=' (operand types are '__gmp_expr<__mpz_struct [1], __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_binary_plus> >' and 'int')
     c = 0;
       ^

有什么问题?

最佳答案

原因是operator+(mpz_class const&, mpz_class const&)不返回另一个 mpz_class , 但中间结果类型 __gmp_expr<T, U> .

source file里面有评论

Results of operators and functions are instances of __gmp_expr<T, U>.

...

Actual evaluation of a __gmp_expr<T, U> object is done when it gets assigned to an mp*_class ("lazy" evaluation): this is done by calling its eval() method.

当你使用 auto c = a + b;你得到 c类型为 __gmp_expr<T, U> ,因此其他mp*_class或整数无法分配给它。

关于c++ - 结合 GMPXX 和 C++11 及更高版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38550299/

相关文章:

c++ - Boost 库在 Windows 上未正确链接

c - 找到最大 n 使得 x^n<=y 的最快算法

c++ - 使用引用时 vector 的意外行为

c++ - 如何区分来自同一子网和不同 IP/用户的 sockaddr_in 结构

c++ - 在QTreeWidget中排序时的调试断言(无效的比较器)

c++ - 如何使用 C++11 创建高效的闭包?

C++11 构造函数参数:std::move 和值或 std::forward 和右值引用

python - gmpy2 : How do I track precision of mpc operations?

c++ - 使用MPFR舍入到整数

c++ - 现在微软基础类(MFC)值不值得学?