Infinite compilation with templates

后端 未结 4 1130
小鲜肉
小鲜肉 2021-01-05 21:07

This question is just out of curiosity. In recursive templates if we forget to put one particular specialization, then compiler will do large number of iterations and then s

4条回答
  •  Happy的楠姐
    2021-01-05 21:58

    Should not be compiler smart enough to stop at some time ?

    How do you define the phrase "at some time"? How would the compiler know your definition of "at some time"? How would it know when it must stop if you don't tell it explicitly? You've to tell it first by defining specialization(s) of the non-stopping class template (what you've written is non-stopping class template).

    In your case, you must have two specializations of the class template, one in each directions (increasing, and decreasing). Something like this:

    template<>
    struct Infinite<100> //this is to stop template with  argument
    {
      enum { value = 678678 }; //see, it doesn't use Infinite<> any further!
    };
    
    template<>
    struct Infinite<-100> //this is to stop template with  argument
    {
      enum { value = -67878 }; //see, it too doesn't use Infinite<> any further!
    };
    

提交回复
热议问题