(N<M) ? commondivs<N,(M-N)>::val : commondivs<(N-M),M>::val
This line causes instantiation of both commondivs<N,(M-N)>::val and commondivs<(N-M),M>::val, even if the condition is known at compile time and one of the branches will never be taken.
Replace ? : with std::conditional_t, which doesn’t have this limitation:
static const int val = std::conditional_t<N < M, commondivs<N,(M-N)>, commondivs<(N-M),M>>::val;