0

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:

7
  • 2
    Discarded: skipped, ignored, as if it wasn't there. Commented Sep 21, 2021 at 14:26
  • Instantiated: used in the context of templates. Means that the template is "embodied" (in fact defined) using the given concrete template arguments. Commented Sep 21, 2021 at 14:28
  • @YvesDaoust: More complicated than that, as explained in point2 and 3. "discarded" is really misleading IMO. Commented Sep 21, 2021 at 14:28
  • 1
    Checked: it needs to be a valid statement anyway. (Even though it can be discarded.) Commented Sep 21, 2021 at 14:32
  • 1
    In fact, point 1 differentiates the two blocks and name the statement/branch which doesn't satisfy the condition, to give it some "properties" afterward. Commented Sep 21, 2021 at 14:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what if a change std::cout << t.find("asd"); in your first code, to whatever contains errors?.
@Roman then its the same. std::cout << t.find("asd"); is an error when t is an int. You can replace it with a different error and still foo<int>() compiles. Only when T == std::string the code in the true-branch is not discarded and checked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.