How to check whether some piece of code causes an error?

  c++, c++20, compiler-errors

Is there some standard function which takes a piece of code and checks whether it causes an error, returning true if the code is valid but false if the code is invalid?

For example:

int x;
bool a_bool = DoesItCauseAnError(constexpr auto y = x;); // Here a_bool is false, as "constexpr auto y = x;" would cause an error as x is not constexpr

This would be immensely useful and would save us all from all the pain of SFINAE and other weird hacks.

  1. Is there a standard function which does this?
  2. If not, why has it not been done already? It would be so useful and seems quite easy to implement.
  3. If there isn’t a standard function, has anybody done a plugin / static tool / compiler specific instruction (preferably gcc or clang) which can do this?

Source: Windows Questions C++

LEAVE A COMMENT