Given the snippet of code below:
template<int n, double m>
void function(int x=n){
double y=m;
int array[n];
….
}
void main () {
function<1+2,2>(8);
}
when the function is compiled is x going to be 3 or 8 (as n is just the default parameter)?
在您的示例中, n
为 3, x
为 8。实际参数值优先于默认值。
what's the benefit of that code!!.
The template non-type parameter must be a structural type (can't be double). See https://en.cppreference.com/w/cpp/language/template_parameters#Non-type_template_parameter
Hence If the double
changed to be int
, the vars would be x=8
, n=3
and m=2
.
Another thing change void main()
to int main()
. See What should main() return in C and C++?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.