cplusplus / CWG

Core Working Group
23 stars 7 forks source link

CWG2887 [diff.cpp03.expr] List behavioral changes of `typeid` and conditional operators in C++11 #528

Open frederick-vs-ja opened 2 months ago

frederick-vs-ja commented 2 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jiang An

Reference (section label): [diff.cpp03.expr]

Link to reflector thread (if any):

Issue description:

From editorial issue cplusplus/draft#6861.

N3055 distinguished xvalues from prvalues in typeid and ternary conditional operators, which changed their behaviors in some code that were well-formed in both C++98 and C++11.

  1. More expressions were made evaluated in typeid (demo). Such expressions are xvalues of polymorphic class types that were already well-formed in C++98.
#include <cstdio>
#include <typeinfo>

int main() {
  struct B {
    B() {}
    virtual ~B() { std::puts("C++11"); }
  };

  struct C { B b; };
  typeid(C().b); // unevaluated in C++98, evaluated in C++11
}
  1. Less copies were made in the conditional operator (demo) if the second and the third operands were xvalues (that were well-formed in C++98) and the result was also an xvalue since C++11.
#include <cstdio>

int main() {
  struct B {
    B() {}
    B(const B&) { std::puts("C++98"); }
  };
  struct D : B {};

  struct BB { B b; };
  struct DD { D d; };

  true ? BB().b : DD().d; // additional copy in C++98, no copy or move in C++11
}

Perhaps we should list the behavior changes in [diff.cpp03.expr].

Suggested resolution:

jensmaurer commented 2 months ago

CWG2887