- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
is_reference Template in C++
In this article we will be discussing the working, syntax and examples of std::is_reference template in C++ STL.
is_reference is a template which comes under <type_traits> header file. This template is used to check whether the given type T is a reference type or not.
This template is a combination of is_rvalue and is_lvalue and checks whether either if one is true, the result of is_reference will be also true.
What is a reference in C++?
A reference is an alias or another name of the already existing variable. A reference is different from pointer −
- As we cannot set a reference as null but a pointer can be a null pointer.
- Once a reference is initialised to an object it cant be changed. Pointers can be pointed to any other object at any time.
- A reference must be initialised when created, where as pointer can be initialised later after creation.
A reference can be declared using an ampersand(&) symbol preceding the variable whom we want to refer.
Syntax
template <class T> is_reference;
Parameters
The template can have only parameter of type T, and check whether the given type is a reference type or not.
Return value
It returns a Boolean value, true if the given type is a reference type, and false if the given type is not a reference type.
Example
Input: is_reference<int>::value; Output: False Input: is_reference<int&>::value; Output: True
Example
#include <iostream> #include <type_traits> using namespace std; class TP { }; int main() { cout << boolalpha; cout << "Checking for is_reference: "; cout << "\n class TP : "<<is_reference<TP>::value; cout << "\n class TP&: "<<is_polymorphic<TP&>::value; cout << "\n class TP&&: "<<is_polymorphic<TP&&>::value; return 0; }
Output
If we run the above code it will generate the following output −
Checking for is_reference: class TP : false class TP&: false class TP&&: false
Example
#include <iostream> #include <type_traits> using namespace std; int main() { cout << boolalpha; cout << "Checking for is_reference: "; cout << "\n int: "<<is_reference<int>::value; cout << "\n int&: "<< is_reference <int&>::value; cout << "\n int&&: "<< is_reference <int&&>::value; // char cout << "\n char: "<<is_reference<char>::value; cout << "\n char&: "<< is_reference <char&>::value; cout << "\n char&&: "<< is_reference <char&&>::value; //float cout << "\n float: "<<is_reference<float>::value; cout << "\n float&: "<< is_reference <float&>::value; cout << "\n float&&: "<< is_reference <float&&>::value; //double cout << "\n double: "<<is_reference<double>::value; cout << "\n double&: "<< is_reference <double&>::value; cout << "\n double&&: "<< is_reference <double&&>::value; return 0; }
Output
If we run the above code it will generate the following output −
Checking for is_reference: int: false int&: true int&&: true char: false char&: true char&&: true float: false float&: true float&&: true double: false double&: true double&&: true