<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 compile code with member constexpr functions"
   href="http://llvm.org/bugs/show_bug.cgi?id=20219">20219</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang fails to compile code with member constexpr functions
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.4
          </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>C++1y
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sarang.baheti@gmail.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>Clang details:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

Hi,

While trying out relaxed constraints on constexpr in C++1y/C++14, I am running
into this odd behavior where member constexpr functions (const qualified) with
constexpr local variables will lead to compilation failure:

Code that fails:

Code.cpp (compile: clang -std=c++1y -Wall code.cpp -o code1y)
---------------------------------------------------

/*
constexpr int gcd_impl(int a, int b)
{
    return b == 0 ? a : gcd_impl(b, a%b);
}

constexpr int max(int a, int b)
{
    return a > b ? a : b;
}

constexpr int min(int a, int b)
{
    return a < b ? a : b;
}

constexpr int gcd(int a, int b)
{
    return a == b ? a : gcd_impl(max(a,b), min(a,b));
}
*/

template<int N, int D>
class Rational
{
private:


    constexpr auto gcd_impl(int a, int b) const
    {
        return b == 0 ? a : gcd_impl(b, a%b);
    }

    constexpr auto max(int a, int b)const
    {
        return a > b ? a : b;
    }

    constexpr auto min(int a, int b)const
    {
        return a < b ? a : b;
    }

    constexpr auto gcd(int a, int b) const
    {
        return a == b ? a : gcd_impl(max(a,b), min(a,b));
    }


public:

    constexpr Rational()
    {}

    template<int N1, int D1>
    constexpr bool operator==(const Rational<N1, D1>&) const
    {
        return N * D1 == D * N1;
    }

    template<int N1, int D1>
    constexpr auto add(const Rational<N1,D1>&) const
    {
        constexpr auto num = N *D1 + D * N1;
        constexpr auto factor = gcd(num, D*D1);
        return Rational<num/factor, D*D1/factor>();
    }

    constexpr int numerator() const
    {
        return N;
    }

    constexpr int denominator() const
    {
        return D;
    }

};


int main()
{
    constexpr auto onehalf = Rational<1,2>();
    constexpr auto onefourth = Rational<1,4>();
    constexpr auto threefourth = onehalf.add(onefourth);

    auto num = threefourth.numerator();
    auto den = threefourth.denominator();

    return 0;
}

fails with error:
------------------------------------------------------------
code.cpp:32:29: fatal error: recursive template instantiation exceeded maximum
depth of 256
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:32:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:32:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:32:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:32:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:32:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:32:29: note: (skipping 247 contexts in backtrace; use
-ftemplate-backtrace-limit=0 to see all)
code.cpp:32:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:32:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
code.cpp:47:29: note: in instantiation of member function 'Rational<1,
2>::gcd_impl' requested here
        return a == b ? a : gcd_impl(max(a,b), min(a,b));
                            ^
code.cpp:66:33: note: in instantiation of member function 'Rational<1, 2>::gcd'
requested here
        constexpr auto factor = gcd(num, D*D1);
                                ^
code.cpp:87:42: note: in instantiation of function template specialization
'Rational<1, 2>::add<1, 4>' requested here
    constexpr auto threefourth = onehalf.add(onefourth);
                                         ^
code.cpp:32:29: note: use -ftemplate-depth=N to increase recursive template
instantiation depth
        return b == 0 ? a : gcd_impl(b, a%b);
                            ^
1 error generated.

If I move comment the constexpr functions defined in Rational class and
uncomment the ones defined outside everything works as expected.
The generated assembly is correct as well

Sarang</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>