我在结合 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 anmp*_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/