[LLVMbugs] [Bug 20175] New: Perfect forwarding hides proper overload

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Jul 1 01:44:33 PDT 2014


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

            Bug ID: 20175
           Summary: Perfect forwarding hides proper overload
           Product: clang
           Version: 3.4
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: akim.demaille at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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

-- 
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/20140701/62bff58d/attachment.html>


More information about the llvm-bugs mailing list