My question is if the compiler will deduce two independent functions, one is for char[4] and the other is for char[3]?
It will.
If I call many func with const char* as above, the compiler will generate many independent functions?
You don't call func with const char*. If you did call func with only const char* then there would be only one instantiation of the function template.
You call the function with const char[4] and const char[3] which do cause separate instantiations. There will be an instantiation for each unique sets of template arguments.
Is this stupid? Should I avoid this?
Depends on use case. In many cases, the optimiser simply expands all calls inline and the instantiations won't leave any trace of their theoretical existance in the generated assembly.
发布评论
评论(1)
会。
您不用
const char*
来调用func
。如果仅使用const char*
调用func
,则功能模板只有一个实例化。您可以使用
const char [4]
和const char [3]
调用该功能,这确实会导致单独的实例。每个唯一的模板参数都会有一个实例化。取决于用例。在许多情况下,优化器只是简单地扩展了所有呼叫内联呼叫,而实例化不会在生成的组装中留下其理论存在的任何痕迹。
It will.
You don't call
func
withconst char*
. If you did callfunc
with onlyconst char*
then there would be only one instantiation of the function template.You call the function with
const char[4]
andconst char[3]
which do cause separate instantiations. There will be an instantiation for each unique sets of template arguments.Depends on use case. In many cases, the optimiser simply expands all calls inline and the instantiations won't leave any trace of their theoretical existance in the generated assembly.