Metaprogramming is another great feature of modern C++ that allows programs to redesign themselves during compilation or run time. In C++17, another new feature of metaprogramming is introduced, logical operation metafunctions. These are variadic metafunctions that are conjunction, disjunction, and negation which can be used for metaprogramming features of applications. In this post, we explain what the negation
(std::negation
) metafunction is.
Table of Contents
What are the logical operation metafunctions in C++?
Modern C++ has metaprogramming abilities which is a programming technique that means a program can be compiled to read, create, analyze, or transfer other program code; or it can compile itself. Metaprogramming can change the way code behaves while running.
In C++17, new variadic metafunctions are released for metaprogramming, these are conjunction
, conjunction_v
, disjunction
, disjunction_v
, and negation
, negation_v
. These traits short-circuit in the metaprogramming sense: template specializations that are not required to determine the result are not instantiated.
What is the negation (std::negation) metafunction in C++?
In C++, negation (std::negation
) or negation_v (std::negation_v
) is a type trait that is defined in the <type_traits>
header that is used to design the logical negation between classes (data types, structs, classes). It is a kind of logical template of NOT on a variadic pack of values.
Here is the simple definition of std::conjunction
.
1 2 3 4 5 6 7 |
template<class _Base> struct negation : integral_constant<bool, !_Base::value> { // negate type of _Base }; |
Is there a simple example of how to use negation in C++?
Here is a simple example about negation in C++.
1 2 3 4 5 6 |
using Int2 = std::integral_constant<int, 2>; using Int4 = std::integral_constant<int, 4>; std::cout << std::negation<std::bool_constant<true>>::value << std::endl; |
1 2 3 4 5 6 7 8 9 |
class baseA {}; class baseB {}; class derivedA : public baseA {}; class derivedB : public baseB {}; std::cout << std::negation_v<std::is_base_of<baseA, derivedA>> << std::endl; |
Is there a full example of how to use negation in C++?
Here is a full example about negation in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <type_traits> class baseA {}; class baseB {}; class derivedA : public baseA {}; class derivedB : public baseB {}; int main() { // Let's define two integral constants using Int2 = std::integral_constant<int, 2>; using Int4 = std::integral_constant<int, 4>; // Let's print the results std::cout << "Negation of Bool: " << std::negation<std::bool_constant<true>>::value << std::endl; std::cout << "Negation of Bool: " << std::negation_v<std::bool_constant<true>> << std::endl; std::cout << "Negation of Classes: " << std::negation_v<std::is_base_of<baseA, derivedA>> << std::endl; std::cout << "Negation of Integrals: " << std::negation_v<std::is_base_of< Int2, Int4>> << std::endl; system("pause"); return 0; } |
For more new details about std::set, you can check this paper P0013R1
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded fro