ブーストの多倍数で少し遊んでみると、次のエラーが発生しました
In file included from main.cpp:1:
In file included from /usr/include/boost/multiprecision/cpp_int.hpp:12:
In file included from /usr/include/boost/multiprecision/number.hpp:16:
In file included from /usr/include/boost/type_traits/is_signed.hpp:15:
In file included from /usr/include/boost/type_traits/is_enum.hpp:14:
In file included from /usr/include/boost/type_traits/intrinsics.hpp:149:
/usr/include/boost/type_traits/is_reference.hpp:32:19: fatal error: recursive template instantiation exceeded maximum
depth of 256
インスタンス化エラーの署名を含む多くの行が続きます。次のコードをコンパイルしたときに問題が発生しました。
#include<boost/multiprecision/cpp_int.hpp>
using big_int = boost::multiprecision::cpp_int;
big_int getOne(big_int){ return (big_int) 1;}
template<typename T, typename U>
T fastPowMod(T a, U b, T p){
if(b==0)
return getOne(a);
if(b%2 != 0){
return (a*fastPowMod(a,b-1,p))%p;
}
else{
T aux = fastPowMod(a,b/2,p);
return (aux*aux)%p;
}
}
int main(){
std::cout << fastPowMod<big_int,big_int>(108041234,180611234, 81243) std::endl;
}
と
clang++ -std=c++11 main.cpp
このコードは、通常の整数でインスタンス化すると完全に正常にコンパイルされるため、なぜこれが起こるのかわかりません。
編集:私は自分自身に答えます。テンプレートと再帰を扱うときは常に明示的であることを忘れないでください!
template<typename T, typename U>
T fastPowMod(T a, U b, T p){
if(b==0)
return getOne(a);
if(b%2 != 0){
return (a*fastPowMod<T,U>(a,b-1,p))%p;
}
else{
T aux = fastPowMod<T,U>(a,b/2,p);
return (aux*aux)%p;
}
}