recursive template instantiation exceeded maximum depth of 256

前端 未结 3 1776
無奈伤痛
無奈伤痛 2021-01-17 17:27

I was trying to rewrite the Factorial implementation using constexpr function but for some reason I have no idea why I get a compile error:

3条回答
  •  盖世英雄少女心
    2021-01-17 18:11

    You need to define a specialization of the template function to stop the recursion at the compile time rather than runtime, just as your struct version did.

    template 
    int f2()
    {
        return N * f2();
    }
    
    template <>
    int f2<0>()
    {
        return 1;
    }
    

提交回复
热议问题