[llvm-bugs] [Bug 33588] New: Recursive variable template function abnormal behaviour when in namespace

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Jun 25 15:06:06 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=33588

            Bug ID: 33588
           Summary: Recursive variable template function abnormal
                    behaviour when in namespace
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: giacomofenzi at outlook.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

I believe I found a bug involving template default parameters, variadic
templates and namespaces. The following code compiles, but it behaves very
differently when the namespace meta is removed. When the namespace is removed,
the code prints the number of elements of type M<> in the parameter list,
otherwise it prints the number of elements of type M<> in the parameter list
until the first element of type M<x> (x!=0) . gcc 7.1 shares the same behavior,
while MSVC prints the (I believe) correct output in both cases.


#include <tuple>

template <size_t x = 0>
class M{};

template <>
class M<0>{};

// Note, if this is removed the problem disappears
namespace meta
{
    inline auto f()
    {
        return std::make_tuple();
    }

    template <std::size_t x, typename ... Args>
    auto f(const M<x>& , Args && ... args)
    {
        return f(args...);
    }

    template <typename ... Args>
    auto f(const M<>& first, Args && ... args)
    {
            return std::tuple_cat(std::make_tuple(first), f(args...));
    }
}

#include <iostream>

int main(int, char* [])
{

    M<1> a;
    M<> b;

    std::cout << std::tuple_size<decltype(meta::f(a, a, a))>::value << "\n"; //
(0, 0)
    std::cout << std::tuple_size<decltype(meta::f(a, a, b))>::value << "\n"; //
(1, 0)
    std::cout << std::tuple_size<decltype(meta::f(a, b, a))>::value << "\n"; //
(1, 0)
    std::cout << std::tuple_size<decltype(meta::f(a, b, b))>::value << "\n"; //
(2, 0)
    std::cout << std::tuple_size<decltype(meta::f(b, a, a))>::value << "\n"; //
(1, 1)
    std::cout << std::tuple_size<decltype(meta::f(b, a, b))>::value << "\n"; //
(2, 1)
    std::cout << std::tuple_size<decltype(meta::f(b, b, b))>::value << "\n"; //
(3, 3)

}

The expected output (and the one we get by removing the namespace) is:
0
1
1
2
1
2
3
While the one we obtain with the namespace is
0
0
0
0
1
1
3

Godbolt test case: https://godbolt.org/g/QfJ2cZ

-- 
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/20170625/d40eacb6/attachment.html>


More information about the llvm-bugs mailing list