Definition of decltype in C++

  1 min read  

Definition of decltype in C++

decltype is a keyword used to query the type of an expression. Introduced in C++ 11, its main use is in generic programming, where it is often difficult, if not impossible, to express types that depend on template parameters.

As generic programming techniques became increasingly popular throughout the 1990s, the need for a type deduction mechanism was recognized. Many compiler vendors have implemented their own versions of the operator, commonly called typeof, and some portable implementations with limited functionality, based on existing language features, have been developed.

In 2002, Bjarne Stroustrup proposed that a standardized version of the operator be added to the C++ language and suggested the name “decltype” to reflect that the operator would produce the “declared type” of an expression.

The semantics was designed to suit both generic library developers and novice programmers. In general, the inferred type matches the type of the object or function exactly as declared in the source code.


#include <iostream>

int the_int(){
  return 3;

int main(){
  //pega automaticamente o tipo de retorno de the_int 
  decltype( the_int() ) x = the_int();
  std::cout << "the value of x is: " << x << '\n';
  return 0;

That’s all for today, small daily doses that will always keep us in tune with C++!

Add our feed: to: