- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Templates and Static variables in C++
In this tutorial, we will be discussing a program to understand templates and static variables in C++.
In case of function and class templates, each instance of the templates has its own local copy of the variables.
Example
#include <iostream> using namespace std; template <typename T> void fun(const T& x){ static int i = 10; cout << ++i; return ; } int main(){ fun<int>(1); //printing 11 cout << endl; fun<int>(2); //printing 12 cout << endl; fun<double>(1.1); //printing 11 again cout << endl; getchar(); return 0; }
Output
11 12 11
- Related Articles
- Class and Static Variables in C#
- Static Variables in C
- Initialization of global and static variables in C
- C++ static member variables and their initialization
- Initialization of static variables in C
- Static and non static blank final variables in Java
- Class and Static Variables in Java
- Kotlin static methods and variables
- Where are static variables stored in C/C++?
- Default values of static variables in C
- Static variables in Java
- How static variables in member functions work in C++?
- Demonstrate static variables, methods and blocks in Java
- Final static variables in Java
- What are the local static variables in C language?

Advertisements