1

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?
SomeProgrammer
  • 1,134
  • 1
  • 6
  • 12
  • No, the closest you can come is to call the compiler to check the code for you. – Ted Lyngmo Mar 02 '21 at 13:37
  • @TedLyngmo That's a shame. Any ideas about why none of the compilers do not support this? – SomeProgrammer Mar 02 '21 at 13:38
  • 1
    Probably because your claim "*... and seems quite easy to implement*" isn't really true. You're basically asking for a compiler embedded inside a compiler that you can call programmatically. That's not going to be easy to do in general. – cigien Mar 02 '21 at 13:40
  • 1
    ... and the usefulness is questionable. I have never lacked this ability. – Ted Lyngmo Mar 02 '21 at 13:41
  • 1
    It won't "save us from SFINAE". Because *how* a condition is checked will not rid you of the need to discard invalid overloads. – StoryTeller - Unslander Monica Mar 02 '21 at 13:42
  • 1
    consider adding a line `auto y = x;` after your call to `DoesItCauseAnError`, now you have `y` declared twice. Who is to blame for it? Is the error `auto y = x;` or is the error `constexpr auto y = x;` ? Also the possibility to nest such conditions would make it incredible complex I believe – 463035818_is_not_an_ai Mar 02 '21 at 13:42
  • Based on my understanding of compilers, the compiler would just parse the "constexpr auto y = x;" as if it was normal code, and if the line caused an error it would just replace DoesItCauseAnError(constexpr auto y = x;); by false. It wouldn't define any new variable – SomeProgrammer Mar 02 '21 at 13:48

2 Answers2

0

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?

It is mathematically impossible to have such function (done at compile time). Read more about Rice's theorem!

In practice, errors do happen at runtime. A simple example is opening a file (e.g. some std::ifstream ...) which does not exist when your run your program.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

You can take a look at the C++20 concepts: the requires keyword allows you to check at compile time if a type fulfill some conditions which would have failed otherwise.

For example, can I std::cout << something ?

using T = int;
if constexpr ( (requires(){std::declval<std::ostream>() << std::declval<T>();}) )
    std::cout << "the tested type is displayable" << std::endl;

However I don't think you can verify the constexpr statement you provided as a block. But you can react to the constexpr property in the code: C++ check if statement can be evaluated constexpr

Cevik
  • 313
  • 1
  • 4
  • 17