typeof and decltype at a glance
Both typeof and decltype find the type of an expression, so you do not have to write the type out by hand. In C, typeof became part of the language in C23. Standard C++ has no typeof keyword. Its counterpart is decltype, which was added in C++11.
Is C++ static or dynamic? C++ is statically typed, so the compiler knows the type of every expression before the program runs. That is why decltype can answer its question without running your code.
typeof in C
The typeof operator takes either an expression or a type name. Here it copies the type of a double variable:
#include <stdio.h>
int main(void) {
double price = 9.5;
typeof(price) total = price * 2; // total is a double
printf("%.2f\n", total); // 19.00
return 0;
}
This needs a compiler that supports C23, such as a recent version of GCC or Clang run with -std=c23. Older versions of GCC and Clang accepted typeof as an extension in C. Some strict modes do not, and there __typeof__ is the spelling to use.
C23 also adds typeof_unqual. It works like typeof, but it drops qualifiers such as const, which helps when you want a plain copy of a value’s type.
What are C standards? They are the published revisions of the language, such as C99, C11 and C23. typeof became standard in C23.
typeof is not standard C++
Standard C++ does not have a typeof keyword. GCC and Clang offer __typeof__ as an extension in C++ code, but code that uses it will not build on every compiler. For C++ that should build anywhere, use decltype instead.
decltype in C++
decltype(expression) gives the type of an expression. The parentheses around the operand and the kind of expression both affect the result:
#include <iostream>
int main() {
int x = 5;
int& ref = x;
decltype(x) a = 10; // int
decltype(ref) b = x; // int&, the declared type of ref
decltype((x)) c = x; // int&, because (x) is an lvalue expression
decltype(x + 1) d = 0; // int, because x + 1 is a new value
std::cout << a << " " << b << " " << c << " " << d << "\n"; // 10 5 5 0
}
The rules come down to three cases:
- A plain name, such as
xorref, gives the type it was declared with. Sodecltype(ref)isint&. - An lvalue expression, which refers to an existing object, gives a reference type. Wrapping a name in parentheses, as in
(x), makes it one, sodecltype((x))isint&. - Any other expression gives its plain type.
x + 1produces a new value, so its type isint.
Parentheses are easy to add by accident, so check them when the exact type matters.
decltype does not run its operand
The expression inside decltype is only checked for its type. It is not evaluated:
#include <iostream>
int main() {
int i = 0;
decltype(i++) next = 0; // i++ is not evaluated here
std::cout << i << " " << next << "\n"; // 0 0
}
The same goes for a call. decltype(make_value()) gives the return type of make_value without calling it.
Using decltype in function templates
A function template often needs a return type that depends on its arguments. A trailing return type with decltype can describe it:
#include <iostream>
template <typename A, typename B>
auto add(A a, B b) -> decltype(a + b) {
return a + b;
}
int main() {
std::cout << add(2, 3) << "\n"; // 5
std::cout << add(1.5, 2) << "\n"; // 3.5
}
Since C++14, you can leave out the return type and let the compiler work it out from the return statement. The version auto add(A a, B b) { return a + b; } does the same job there.
decltype(auto)
decltype(auto) is a C++14 feature. It uses the decltype rules to decide the type of a variable:
int x = 1;
int& r = x;
auto a = r; // int: auto drops the reference
decltype(auto) b = r; // int&: decltype(auto) keeps it
Use auto when you want a copy of a value. Use decltype(auto) when you need the exact type that decltype would report.
What is the type of a lambda?
Each lambda expression creates its own unnamed class type, so you cannot write its name in your code. decltype gives you a way to refer to that type:
#include <iostream>
int main() {
auto square = [](int n) { return n * n; };
decltype(square) copy = square; // the same type as square
std::cout << copy(4) << "\n"; // 16
}
Common mistakes
- Adding parentheses by accident.
decltype(x)isint, butdecltype((x))isint&. - Expecting
autoanddecltypeto match.autodrops references and top-levelconst. Withconst int& cr = x;,auto a = cr;givesint, whiledecltype(cr)isconst int&. - Using
typeofin portable C++. Standard C++ does not include it. Usedecltype. - Counting on side effects inside
decltype. The operand is not evaluated, soi++inside it does not changei.
Quick reference
| Form | Where it works | Result |
|---|---|---|
typeof(expression) |
C23 | Type of the expression, with its qualifiers |
typeof(type-name) |
C23 | The named type |
typeof_unqual(expression) |
C23 | Type of the expression without qualifiers such as const |
__typeof__(expression) |
GCC and Clang, in C and C++ | Like typeof, as an extension |
decltype(expression) |
C++11 | The declared type of a name, T& for an lvalue, T for other expressions |
decltype(auto) |
C++14 | A variable type chosen by the decltype rules |
For C++ code, use auto when you want a copy of a value’s type, and decltype when you need the exact type, references and const included. For C code, typeof is standard from C23 onward, and typeof_unqual is there for a plain type without qualifiers.