Skip to content

Trivia impact: none

The Type You Can Call

a type trait that behaves like a nullary function

std::cout << std::boolalpha << std::true_type{}() << '\n';

Can a temporary type trait be called with parentheses?

Answer
true

std::true_type{}() is a valid function call and returns its stored value.

Why

std::true_type is an alias for an std::integral_constant specialization. [meta.help] gives integral_constant both a conversion operator and a nullary operator() returning value. The callable spelling has been present since C++14, so it is ordinary C++17 rather than a compiler extension.

Where it shows up

Almost nowhere on purpose. Older template-metaprogramming code may use it where a callable predicate is convenient, but reading ::value is usually clearer.

Takeaway: An std::integral_constant specialization supplies a value and a nullary callable.

Try it: g++ -std=c++17 main.cpp -o demo && ./demo

Open in Compiler Explorer ↗ Quiz this entry