[LLVMbugs] [Bug 22639] New: clang fails to propagate constness to union member in template

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Feb 19 17:42:34 PST 2015


            Bug ID: 22639
           Summary: clang fails to propagate constness to union member in
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: kaballo86 at hotmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150220/7d7d69e7/attachment.html>

More information about the llvm-bugs mailing list