Skip to content

Instantly share code, notes, and snippets.

@mnzk
Created October 16, 2011 01:38
Show Gist options
  • Save mnzk/1290403 to your computer and use it in GitHub Desktop.
Save mnzk/1290403 to your computer and use it in GitHub Desktop.
gimpxx で三項演算子が爆発した
// gmpxx : mpz_class で三項演算子が使えない(解決した)
#include <iostream>
#include <gmpxx.h>
// これなら OK
// mpz_class next_collatz(const mpz_class &n){
// if(n % 2 == 0){
// return n >> 1;
// }else{
// return n * 3 + 1;
// }
// }
//これだとコンパイルエラー (compile error)
// mpz_class next_collatz(const mpz_class &n){
// return n % 2 == 0 ? n >> 1 : n * 3 + 1;
// }
//これで通った OK
mpz_class next_collatz(const mpz_class &n){
return n % 2 == 0 ? mpz_class(n >> 1) : mpz_class(n * 3 + 1);
}
int main(int argc, char**argv){
if(argc<2) return -1;
mpz_class n(argv[1]);
for( ;n!=1; n = next_collatz(n)) std::cout << n << " ";
std::cout << n << std::endl;
return 0;
}
/*
$gcc -v
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
$gcc collatz.cpp -o collatz -lgmpxx
collatz.cpp: In function ‘mpz_class next_collatz(const mpz_class&)’:
collatz.cpp:13: error: no match for ternary ‘operator?:’ in ‘operator== [with T = __mpz_struct [1], U = __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, long int, __gmp_binary_modulus>](((const __gmp_expr<__mpz_struct [1], __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, long int, __gmp_binary_modulus> >&)((const __gmp_expr<__mpz_struct [1], __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, long int, __gmp_binary_modulus> >*)(& operator% [with T = __mpz_struct [1], U = __mpz_struct [1]](((const __gmp_expr<__mpz_struct [1], __mpz_struct [1]>&)((const mpz_class*)n)), 2)))), 0) ? operator>> [with T = __mpz_struct [1], U = __mpz_struct [1]](((const __gmp_expr<__mpz_struct [1], __mpz_struct [1]>&)((const mpz_class*)n)), 1ul) : operator+ [with T = __mpz_struct [1], U = __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, long int, __gmp_binary_multiplies>](((const __gmp_expr<__mpz_struct [1], __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, long int, __gmp_binary_multiplies> >&)((const __gmp_expr<__mpz_struct [1], __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, long int, __gmp_binary_multiplies> >*)(& operator* [with T = __mpz_struct [1], U = __mpz_struct [1]](((const __gmp_expr<__mpz_struct [1], __mpz_struct [1]>&)((const mpz_class*)n)), 3)))), 1)’
Compilation exited abnormally with code 1 at Sun Oct 16 10:24:51
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment