You are here:

C++/using template in c++


i am just a beginner in c++ . when i use the following code
it says that template cannot be used in function main
is there a way to use templates in function main.
#include <iostream>
using namespace std;
template <class T>
int main()
T take;

Yes you can use templates (as instantiated specialisations of those templates) in main but you have _not_ used a template _in_ function main. You have declared main to _be_ a function template.

Templates for functions or classes are _not_ functions or a classes. They are a recipes for making (i.e. templates for) a whole family of functions or classes.

So until such a (specialisation of a) template is instantiated there is no function or class. That is until you apply a template with a set of specific template parameters and cause the compiler to or explicitly instantiate the template (specialisation) there is no function or class.

So in your case you have the template for a template-parametrised main function, but no concrete specialisation instantiated for that template. You would have to specifically define and instantiate such a specialisation, e.g.:

   template int main<int>();

The problem with doing so is that the main function produced via an explicit template instantiation counts as an overload of the usual, non-templated main function(s), and this is _explicitly_ forbidden in the ISO C++ standard - section 3.6.1 Main function, paragraph 2 has the words:

   "This function shall not be overloaded."

Some compilers will say you cannot overload main in this case while others will catch what you are trying to do and explicitly state you cannot declare main to be a template.

In short do not do this. It seems fairly pointless anyway as each program can only have _one_ instance of main anyway.

Leave main as a simple entry point to the program that is called by the program startup code. Instead move your function template to something that is called from within main, for example:

   template <typename T>
   void DoTheWork()
       T take;

   int main()

In fact in general most program logic should be moved out of main. Main should be reserved for program initialisation and uninitialisation, command line processing and top level (last chance!) error handling. Even here any stage that looks like getting too complex should be considered for refactoring out of main - e.g. if command line argument handling is getting overly complex and verbose move it to its own function or maybe even one or more helper classes.

Hope this fixes your problem.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.