user15071942
user15071942

Reputation:

Behavior of If constexpr in C++

Even having a lot a questions about this topic, I am having more and more problems. I think the problem is in understanding the meaning of certain words. All quotes below are from Cppreference

  1. What does 'discarded' mean in the text below? I understand 'discarded' as something never compiled/touched, something that, whatever it is (like random characters that would be errors), it won't interfere in the rest of the program.

In a constexpr if statement, the value of condition must be a contextually converted constant expression of type bool (until C++23)an expression contextually converted to bool, where the conversion is a constant expression (since C++23). If the value is true, then statement-false is discarded (if present), otherwise, statement-true is discarded.

  1. What 'instantiaded' mean ?

If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated .

  1. What 'checked' mean? I understand that 'checked' means the code was totally compiled and verified for any possible error at that time.

Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive:

Upvotes: 0

Views: 919

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122133

Consider this example:

#include <iostream>
#include <string>

template <typename T>
void foo() {
    T t;
    if constexpr (std::is_same_v<T,std::string>){
        std::cout << t.find("asd");
    } else {
        t = 0;
        std::cout << t;
    }
}

int main () {
    foo<int>();                     // (2)
}

When T is a type that does not have a find method, then std::cout << t.find("asd") is an error. Nevertheless, the template is ok.

  1. What 'instantiaded' mean ?

The template is instantiated in (2). foo is just a template, instantiating it results in a function foo<int> that you can call.

  1. What does 'discarded' mean in the text below?

The true-branch is discarded when foo<int> is instantiated (because the condition is false). Hence, even though int has no find method, the code compiles without error.

Now consider this similar, but very different example:

#include <iostream>

int main () {
    int x = 0;
    if constexpr (true) {
        std::cout << x;
    } else {
        x.find("asd");
    }
}
  1. What 'checked' mean?

The text is a little contrived, it says that in the above example the false branch is discarded, but nevertheless it is checked, because it is outside of a template. It is just the english term: "checked" means the compiler checks if the code is correct. int has no method find, hence the above results in an error:

<source>:8:15: error: request for member 'find' in 'x', which is of non-class type 'int'
    8 |             x.find("asd");
      |               ^~~~

Even though this statement is never executed, it must be valid code.

Upvotes: 1

Related Questions