Passing a variable as a template argument

前端 未结 6 1845
[愿得一人]
[愿得一人] 2020-11-28 11:02

I am working with a library which exposes an interface to work with. One of the functions of this library is like this :

template 
void modify()         


        
6条回答
  •  有刺的猬
    2020-11-28 11:50

    1. "Why can't compiler evaluate i at compile time?"

      That would defeat the purpose of templates. Templates are there for the case where the source code looks the same for some set of cases, but the instructions the compiler needs to generate are different each time.

    2. "Is there any other to achieve the objective I am trying to achieve without changing the API interface?"

      Yes, look at Boost.MPL.

      However I suspect the right answer here is that you want to change the API. It depends on the internals of the modify function. I know you have it's source, because templates must be defined in headers. So have a look why it needs to know i at compile time and if it does not, it would be best to replace (or complement if you need to maintain backward compatibility) it with normal function with parameter.

提交回复
热议问题