<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 --- - linker error passing const static field as template argument"
   href="http://llvm.org/bugs/show_bug.cgi?id=19229">19229</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>linker error passing const static field as template argument
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>jds.register@icloud.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=12268" name="attach_12268" title="LLVM IR of sample">attachment 12268</a> <a href="attachment.cgi?id=12268&action=edit" title="LLVM IR of sample">[details]</a></span>
LLVM IR of sample

The error is reproducible with:

struct MyStr {
  static const int ConstVal = 1;
  int AnInt;
};

static bool operator==(MyStr LHS, int RHS) { return LHS.AnInt == RHS; }

template <class T, class U>
bool operator!=(const T& LHS, const U& RHS) { return !(LHS == RHS); }

void foo() {
  MyStr S { MyStr::ConstVal };
  bool b1 = S == MyStr::ConstVal;
  // Actually only this line is needed for the error:
  bool b2 = S != MyStr::ConstVal;
}

The code compiles fine, but the linking stage fails:

Undefined symbols for architecture x86_64:
  "__ZN5MyStr8ConstValE", referenced from:
      __Z3foov in main.o

I'm using the trunk version, but the error occurred in Xcode as well.

I looked myself around a bit and found the following:
If you look at the LLVM IR (see my attachment; only mem2reg applied, to save a
few lines), there are two things to notice:
First: @_ZN5MyStr8ConstValE is defined as an external i32 (the origin of the
linker error!)
Second: Although @_Zeq5MyStri (operator==) is called directly with the 1 (line
21) and the variable S is directly initialized/build up with the 1 (line 7 &
14/15), @_ZneI5MyStriEbRKT_RKT0_ (operator!=) is called with a pointer to
@_ZN5MyStr8ConstValE. Why is not simply passed 1 here as well?</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>