Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty angle brackets in template definition

Tags:

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.

like image 903
Netherwire Avatar asked May 21 '14 09:05

Netherwire


People also ask

What means empty template?

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.

What is C++ template?

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++.


1 Answers

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.

like image 110
ForEveR Avatar answered Sep 19 '22 15:09

ForEveR