This question was at the interview: Does this code causes any compile/linking errors and why so?
template <int T> void f(); template <> void f<0>() {} void test() { f<1>(); }
Please explain the behaviour. Thanks a lot.
It means that default template arguments are used. For example, if you had a template : template < typename T = int > struct A; then this would have type int for the template argument : A<> a; Follow this answer to receive notifications.
Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. To simply put, you can create a single function or single class to work with different data types using templates. C++ template is also known as generic functions or classes which is a very powerful feature in C++.
template<> void f<0>() {}
is specialization of function template for argument 0
, if you call f<0>()
this version of function will be called.
This code is incorrect, it cause linking errors, since there is no specialization for f<1>
and template version of function is not defined
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With