mardi 27 avril 2021

C++ determining at compile time whether a particular operation on one or more types is valid

In C++, for C++11 on up, I want to be able to determine whether a particular operation on one or more types is valid at compile time, with a boolean value of 'true' indicating that the operation is valid and a value of 'false' indicating that the operation is not valid.

In the Boost.TypeTraits library there are operations as part of the library, called "Operator Type Traits", which can determine this for me for a very large range of operators. I am not questioning the value of this code, for it is excellent. It was written to work at the C++03 level, but does have a few limitations. Does some C++, from C++11 on up, have compile time support for such a determination as part of the C++ enhancements to that particular level of C++ ? As an example, given type T and type U, I want to create a compile time question of whether A + B is a valid operation. I can not say decltype(T() + U()) because if this is not valid I will get a compile time error. Instead I want to write code which basically says "if decltype(T() + U())" is valid do this, else do that. Is this possible ?

Aucun commentaire:

Enregistrer un commentaire