1

I am reading about ODR-use and I encountered this:

a variable x in a potentially-evaluated expression ex is odr-used unless both of the following are true:

What is a potentially-evaluated expression?

Update: I might just found the answer when rolling down the page:

In the definitions above, potentially-evaluated means the expression is not an unevaluated operand (or its subexpression), such as the operand of sizeof and a set of potential results of an expression e is a (possibly empty) set of id-expressions that appear within e, combined as follows:

But not sure if this is what it means in general + I do not really understand their explanation anyway. Also, why to say potential result instead of just result?

1 Answers1

5

In simplest terms, potentially-evaluated is exactly what it says it is: the expression has a chance to be evaluated. This includes anything except sizeof(ex), decltype(ex), typeid(ex) and noexcept(ex). Other than in these contexts, ex is potentially-evaluated.

In relation to odr-used, it means x is considered odr-used only if ex is potentially-evaluated. That is to say, the necessary condition for x to be odr-used is that ex must be potentially-evaluated. This enables things such as

struct S
{
    static float f;  // declared but not defined
};

decltype(&S::f) p1;  // since &S::f isn't potentially evaluated, this is well-formed

float* p2 = &S::f;  // this is ill-formed
Passer By
  • 19,325
  • 6
  • 49
  • 96
  • is "not potentially evaluated" equivalent with "unevaluated context"? – bolov Jul 03 '17 at 19:35
  • @bolov [I would guess so](https://stackoverflow.com/questions/35088599/what-are-unevaluated-contexts-in-c). To be sure, I don't know where "unevaluated context" appears so I'm not 100% sure. It doesn't appear in either [[expr](http://eel.is/c++draft/expr#def:unevaluated_operand)] nor [[basic.def.odr](http://eel.is/c++draft/basic.def.odr)] – Passer By Jul 03 '17 at 19:37
  • @PasserBy Search for unevaluated operands. – skypjack Jul 03 '17 at 19:50
  • "It means x is considered odr-used only if ex is potentially-evaluated" but what does "potentially-evaluated" mean? –  Jul 03 '17 at 20:24
  • @NeiLee I'm bad at explaining this, hopefully the reworded version is better – Passer By Jul 04 '17 at 05:52
  • Well it helped but I still do not get why expression can be considered only potentially evaluated and not just evaluated or not evaluated? –  Jul 04 '17 at 12:01
  • 1
    @NeiLee Because a potentially-evaluated expression might not end up being evaluated too, for instance if you have a tautology in a branch, the contents of the branch is still considered potentially-evaluated. – Passer By Jul 04 '17 at 12:03
  • Ah I think I get it, but what would make an expression that usually would be evaluated to not get evaluated? Since they only are potentially-evaluated, like "float* p2 = &S::f;", would this not always be evaluated or what? –  Jul 04 '17 at 12:09
  • @NeiLee Yes `float* p2 = &S::f` is always evaluated, but that doesn't conflict with the fact it is potentially-evaluated? – Passer By Jul 04 '17 at 12:12
  • Okay then this is exactly what still confuses me a little; If this expression always is evaluated, why say that it is **potentially**-evaluated? –  Jul 04 '17 at 12:25
  • @NeiLee A certainly evaluated expression is just a subset of potentially-evaluated expressions? Since we only wanted the properties of a potentially-evaluated expression, we don't bother with saying it is certainly evaluated. – Passer By Jul 04 '17 at 12:27
  • @user8221510 A potentially evaluated expression is not necessarily evaluated as can be seen in [\[basic.def.odr\]/(2.6)](http://eel.is/c++draft/basic.def.odr#2.6) and [\[expr.cond\]/1](http://eel.is/c++draft/expr.cond#1). – Belloc Aug 24 '18 at 23:40
  • For "potentially" part: IIUC `ex2` is potentially-evaluated in `true ? ex1 : ex2`, even it is never evaluated. – VainMan Jun 23 '22 at 08:03