Typename Keywords in C++
Typename keywords in C++ are used to tell the compiler that a dependent name(A name that depends on the value of the template parameter) in a template is a type. These are always used alongside template keywords in C++.
Typename replaces “class” in a template keyword in C++ syntax. There is no significant difference between typename and class for a compiler.
Syntax
template<typename X>
class <class name>{<code>}
Typename has made the “X” a dependent type from a dependent name.
Usage of Typename Keywords in C++
We use typename keywords in C++ to define the type of a dependent name in a template. It also specifies that an expression refers to a template function.
In the next section, we will dive into the details of the usage of these keywords.
Defining Template
We can use template keywords in C++ and typename keywords in C++ to define templates in C++.
Example
We will discuss the below given example to calculate the max between two numbers of different data types to explain how to define templates.
#include <iostream>
using namespace std;
// This function will work for all data types, following the DRY principle.
template <typename Z> Z calculateMax(Z x, Z y)
{
return (x > y) ? x : y;
}
int main()
{
// Calling calculateMax for int type
cout << calculateMax<int>(5, 9) << end1;
// Calling calculateMax for double type
cout << calculateMax<double>(5.0, 9.0) << end1;
// Calling calculateMax for char type
cout << calculateMax<char>('a', 'h')<< end1;
return 0;
}
Output
Explanation
We have used the typename keyword in C++ here to specify the dependent name “Z” type. Notice how we have called out the “calculateMax” template under the main function, which is used for all data types. The compiler expands the template while executing the code and uses it to evaluate the results of the main function.
Try and compile by yourself with the help of online C++ Compiler for better understanding.
Defining Template Functions
We use template keywords in C++ and typename keywords in C++ to define template functions in C++.
Suppose you want to mix the lower triangular half of a matrix with another matrix, and we don’t use template and typename keywords in C++ for the program. The code will only work for dynamic-sized matrices with single-precision(32-bit memory location) numbers. To develop a code that is flexible and thus can be used for the static size of a matrix with double-precision(64-bit memory location), we must use the template keywords in C++ with typename keywords.
Example
We will discuss the below given example to of different data types to explain how to define template functions.
NOTE: we will use the eigen library for matrix operations.
#include <Eigen/Dense>
#include <iostream>
template <typename Derived1, typename Derived2>
void mixingTwoMatrix(Eigen::MatrixBase<Derived1>& dst, const Eigen::MatrixBase<Derived2>& src)
{
dst.template triangularSectionMatrix<Eigen::Upper>() = src.template triangularSectionMatrix<Eigen::Upper>();
}
int main()
{
Eigen::MatrixXi matrix1 = Eigen::MatrixXi::Ones(5,5);
Eigen::MatrixXi matrix2 = Eigen::MatrixXi::Random(4,4);
std::cout << "matrix2 before mixing:" << std::end1;
std::cout << matrix2 << std::end1 << std::end1;
mixingTwoMatrix(matrix2, matrix1.topLeftCorner(4,4));
std::cout << "matrix2 after mixing:" << std::end1;
std::cout << matrix2 << std::end1 << std::end1;
}
Output
Explanation
Only template and typename keywords in C++ can give us the accurate result of this program. This is because when a compiler compiles this code, it will expand the template under the main function, wherever mentioned before evaluating the parameters(derived parameter values in the above example). This leads to evaluating any size of a matrix with any type of numeric value.
Now it's time to discuss some FAQs.
Frequently Asked Questions
Who developed the C++ programming language?
Bjarne Stroustrup developed this widely used C++ programming language. He started developing it in 1979 when he joined the technical staff and Bell Laboratories.
How to install the Eigen library?
You can download the Eigen library for C++ just like any other application or library from the browser. Download the Eigen library from the official website and unzip the file at the desired location. Then download the modules, and you’re good to go.
What are keywords in C++?
Keywords are library-defined words that have a special meaning in C++. They are always written in lower cases and cannot be used as variables.
Who is the developer of Eigen?
Benoît Jacob and Gaël Guennebaud developed the Eigen library. This library was released in December 2006.
Can template classes have a constructor?
Yes, we can have a constructor in template classes. We can call the default constructor for a template function.
Conclusion
This article discussed what template keywords in C++ and typename keywords in C++ are. We also learned how to use these keywords by defining template and template functions in C++ to avoid errors and to follow the DRY principle.
To learn more about the C++ programming language, refer to,
Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.
Happy Learning!