Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-12-20


734. Are unique addresses required for namespace-scope variables?

Section:7.6.1.10  [expr.reinterpret.cast]    Status:CD2    Submitter:Daveed Vandevoorde    Date:15 October, 2008

[Voted into WP at March, 2010 meeting.]

Consider the following example:

    static const char test1 = 'x';    static const char test2 = 'x';    bool f() {        return &test1 != &test2;    }

Isf() allowed to returnfalse? Can a smartoptimizer alias these two variables, taking advantage of the fact thatthey areconst, initialized to the same value, and thus cannever be different in a well-defined program?

The C++ Standard doesn't explicitly specify address allocation ofobjects except as members of arrays and classes, so the answer wouldappear to be that such an implementation would be conforming.

This situation appears to have been the inadvertent result ofthe resolution ofissue 73. Prior to thatchange, 7.6.10 [expr.eq] said,

Two pointers of the same type compare equal if and only if they...both point to the same object...

That resolution introduced the current wording,

Two pointers of the same type compare equal if and only if... bothrepresent the same address.

Notes from the March, 2009 meeting:

The CWG agreed that this aliasing should not be permitted ina conforming implementation.

Proposed resolution (November, 2009):

  1. Add the following as a new paragraph after 6.8.2 [intro.object] paragraph 5:

  2. Unless an object is a bit-field or a base class subobjectof zero size, the address of that object is the address of thefirst byte it occupies. Two distinct objects that are neitherbit-fields nor base class subobjects of zero size shall havedistinct addresses [Footnote: Under the “as-if”rule an implementation is allowed to store two objects at thesame machine address or not store an object at all if the programcannot observe the difference (6.10.1 [intro.execution]). —end footnote].[Example:

      static const char test1 = 'x';  static const char test2 = 'x';  const bool b = &test1 != &test2;   // always true

    end example]

  3. Change 7.6.2.2 [expr.unary.op] paragraph 3 as follows:

  4. The result of the unary& operator is a pointer toits operand. The operand shall be an lvalue or aqualified-id.In the first case, if the type of theexpression is “T,” the type of the result is“pointer toT.” In particular, the addressof an object of type “cvT” is“pointer tocvT,” with the samecv-qualifiers. For aqualified-id, if the member is astatic member of type “T”, the type of theresult is plain “pointer toT.” If themember is a non-static member of classC of typeT, the type of the result is “pointer to member ofclassC of typeT.”If theoperand is aqualified-id naming a non-static memberm of some classC with typeT, theresult has type “pointer to member of classC oftypeT” and is an rvalue designatingC::m. Otherwise, if the type of the expression isT, the result has type “pointer toT” and is an rvalue that is the address of thedesignated object (6.8.1 [intro.memory]) or a pointer to thedesignated function. [Note: In particular, the address ofan object of type “cvT” is“pointer tocvT,” with the samecv-qualification. —end note] [Example:...



[8]ページ先頭

©2009-2026 Movatter.jp