Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?

前端 未结 4 550
天涯浪人
天涯浪人 2020-11-22 07:50

The C++ standard contains a semi-famous example of \"surprising\" name lookup in 3.3.2, \"Point of declaration\":

int x = x;

This initializ

4条回答
  •  旧时难觅i
    2020-11-22 08:08

    The behavior is not undefined. The variable is uninitialized and stays with whatever random value uninitialized values start up with. One example from clan'g test suit:

    int test7b(int y) {
      int x = x; // expected-note{{variable 'x' is declared here}}
      if (y)
        x = 1;
      // Warn with "may be uninitialized" here (not "is sometimes uninitialized"),
      // since the self-initialization is intended to suppress a -Wuninitialized
      // warning.
      return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
    }
    

    Which you can find in clang/test/Sema/uninit-variables.c tests for this case explicitly.

提交回复
热议问题