0

I had expected the compiler to allow invalid or error statements in unreachable code involving constexpr if:

#include <type_traits>
#include <iostream>

struct ret_t;
struct only_declared_t;

auto test = [](auto a) {
    if constexpr(std::is_invocable_r_v<ret_t, decltype(a), int>) {
        return a(42);
    } else {
        return 42;
    }

    // I expect to never reach that statement, but the compiler complains
    static_assert(false, "This code should never be reached."); 
    return only_declared{};
};

int main() {
    std::cerr << "Result is " << test(10) << "\n";
}

Any explanation/workaround would be helpful

Markus W.
  • 307
  • 3
  • 10
  • 1
    The compiler has to keep parsing the code, if for nothing else just to find out where the function ends. – Guy Incognito Sep 15 '20 at 09:50
  • As for a workaround: throw an exception instead of static_assert? – freakish Sep 15 '20 at 09:53
  • you can put `static_assert`s almost anywhere, they need not be "executed" to cause an error – 463035818_is_not_an_ai Sep 15 '20 at 09:53
  • as a workaround, just remove it? No code after the `else` is reachable – 463035818_is_not_an_ai Sep 15 '20 at 09:54
  • Basically this https://stackoverflow.com/questions/53818624/why-does-if-constexpr-require-an-else-to-work - But even then, you can't just unconditionally have a false static assertion https://stackoverflow.com/questions/53945490/how-to-assert-that-a-constexpr-if-else-clause-never-happen - Templates aren't macros, they are part of the type system. And `if constexpr` doesn't allow token soup, it still has constraints – StoryTeller - Unslander Monica Sep 15 '20 at 10:00
  • You 'had expected the compiler to allow invalid or error statements in unreachable code' why? 'Unreachable' at runtime doesn't mean the compiler doesn't have to compile it, and certainly doesn't mean it doesn't have to be syntactically or semantically correct. – user207421 Sep 15 '20 at 10:28

1 Answers1

-2

static_assert works in compile time, so it generates compile-time errors regardless of code reachability. You can use assert for run-time checks. assert shall not be evaluated.

Michael Lukin
  • 829
  • 3
  • 9
  • 19
  • It has nothing to do with `static_assert`. *All* code gets compiled regardless of reachability. – user207421 Sep 15 '20 at 10:53
  • @MarquisofLorne, you are absolutely right. – Michael Lukin Sep 15 '20 at 11:04
  • This is factually wrong to a horrendous extent. Please don't guess at an answer, it misleads people. – Passer By Sep 15 '20 at 12:03
  • @PasserBy C++ standard says: In a static_assert-declaration, the constant-expression shall be a contextually converted constant expression of type bool. If the value of the expression when so converted is true, the declaration has no effect. Otherwise, the program is ill-formed, and the resulting diagnostic message shall include the text of the string-literal, if one is supplied, except that characters not in the basic source character set are not required to appear in the diagnostic message. What is factually wrong? – Michael Lukin Sep 15 '20 at 13:19
  • The quote in your comment is fine. Your answer on the other hand, is not clear at all. – cigien Sep 15 '20 at 17:03