<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - clang fails to propagate constness to union member in template"
   href="http://llvm.org/bugs/show_bug.cgi?id=22639">22639</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang fails to propagate constness to union member in template
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kaballo86@hotmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In the following snippet, clang doesn't propagate constness to the union member
in the marked line:

    template <int I> struct _index {};

    struct u1 {
      u1(_index<0>, float v) : _head(v) {}

      void get(_index<0>) {}
      float const& get(_index<0>) const { return _head; }

      float _head;
    };

    template <typename = void> // must be template
    struct u2 {
      u2(_index<0>, int v) : _head(v) {}
      u2(_index<1>, float v) : _tail(_index<0>{}, v) {}

      void get(_index<0>) {}
      int const& get(_index<0>) const { return _head; }

      void get(_index<1>) {}
      float const& get(_index<1>) const { return _tail.get(_index<0>{}); } //
fails here
      //float const& get(_index<1>) const { return
this->_tail.get(_index<0>{}); } // this works

      union {
        int _head;
        u1 _tail;
      };
    };

    struct variant {
      variant(int i) : _storage{_index<0>{}, i}, _which(0) {}
      variant(float f) : _storage{_index<1>{}, f}, _which(1) {}

      template <int I> void const* target() const
      { return _which == I ? &_storage.get(_index<I>{}) : nullptr; }

      u2<> _storage;
      int _which;
    };

    int main() {
      variant const sv(42.f);
      sv.target<1>(); // note: requested here
    }

This can be observed via the error message `reference to type 'const float'
could not bind to an rvalue of type 'void'`, showing that the non-const
overload of `_tail.get` was chosen. Using `this->_tail` instead works as
expected.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>