<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Recursive variable template function abnormal behaviour when in namespace"
   href="https://bugs.llvm.org/show_bug.cgi?id=33588">33588</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Recursive variable template function abnormal behaviour when in namespace
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>giacomofenzi@outlook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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: <a href="https://godbolt.org/g/QfJ2cZ">https://godbolt.org/g/QfJ2cZ</a></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>