[LLVMbugs] [Bug 20219] New: clang fails to compile code with member constexpr functions

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun Jul 6 00:04:18 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20219

            Bug ID: 20219
           Summary: clang fails to compile code with member constexpr
                    functions
           Product: clang
           Version: 3.4
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++1y
          Assignee: unassignedclangbugs at nondot.org
          Reporter: sarang.baheti at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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

-- 
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/20140706/9967e0fa/attachment.html>


More information about the llvm-bugs mailing list