<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 --- - Perfect forwarding hides proper overload"
href="http://llvm.org/bugs/show_bug.cgi?id=20175">20175</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Perfect forwarding hides proper overload
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.4
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</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++11
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>akim.demaille@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>See also #20174 which was my early attempt to reproduce a failure I have with
clang on my project. The key ingredient I was lacking is using const& in my
out() routines below.
There are three classes. The base class roughly represents some parametric
container that will be parameterized by a tuple<char, char>. The middle class
aggregates an instance of the base class, and only forwards the calls to this
base class. The derived class specializes the decorator by looking at the
first item of the tuple only.
This program is valid: main invoke a routine of derived passing it a char
argument. However, clang++ seems to miss the fact that the proper routine to
call is the one defined in derived, and prefers the perfect forwarding from
decorator, which pushes it to call the routine from base which is completely
wrong from the type-checking point of view, since it requires a tuple as
argument.
The program below behaves as expected with g++ 4.8 and 4.9.
$ cat /tmp/bar.cc
#include <string>
#include <iostream>
#include <tuple>
template <typename Word>
struct base
{
using word = std::tuple<char, char>;
int out(int s) const
{
return s;
}
int out(int s, const word& w) const
{
std::cerr << "Tuple: " << std::get<0>(w) << std::get<1>(w) << '\n';
return s;
}
};
template <typename Aut>
struct decorator
{
decorator(Aut a)
: a_(a)
{}
Aut a_;
template <typename... Args>
auto
out(Args&&... args)
-> decltype(a_.out(args...))
{
return a_.out(args...);
}
};
template <size_t Tape, typename Word>
struct derived : decorator<base<Word>>
{
using super_t = decorator<base<Word>>;
using word = typename std::tuple_element<Tape, Word>::type;
using super_t::out;
using super_t::super_t;
int out(int s, const word& w)
{
std::cerr << "char: " << w << '\n';
return s;
}
};
int main()
{
using word = std::tuple<char, char>;
base<word> b;
derived<0, word> d(b);
int s = 12;
char l = 'a';
d.out(s, l);
}
$ clang++-mp-3.5 --version
clang version 3.5.0 (trunk 210448)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
$ clang++-mp-3.5 bar.cc -std=c++11
$ ./a.out
Tuple: a
$ g++-mp-4.9 bar.cc -std=c++11
$ ./a.out
char: 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>