[llvm-bugs] [Bug 33195] Incorrect arguments matching in a template class constructor

via llvm-bugs llvm-bugs at lists.llvm.org
Sun May 28 13:26:36 PDT 2017


Richard Smith <richard-llvm at metafoo.co.uk> changed:

           What    |Removed                     |Added
             Status|NEW                         |RESOLVED
                 CC|                            |richard-llvm at metafoo.co.uk
         Resolution|---                         |INVALID

--- Comment #1 from Richard Smith <richard-llvm at metafoo.co.uk> ---
This code is ill-formed.

Default argument instantiation is not in the immediate context of the
substitution (confirmed recently in committee discussion of core issue 2202).
During overload resolution, the copy constructor of Outer is considered, using
{{1, "a"}, {2, "b"}} to initialize a temporary Outer object, and while
considering the initialization of that temporary object, we consider
initializing a Middle object from the expression 1, which results in
instantiating the default argument expression for the 

  Middle(const int&, const T& = {})

constructor (with T = Inner). That instantiation fails, because {} cannot be
converted to type Inner. You can see the same thing by trying this:

  bool k = std::is_constructible<Middle<Inner>, int &&>::value;

Essentially, by writing a templated function with a default argument that fails
to instantiate, you have written non-SFINAE-friendly code, which in effect sets
up a trap that will render any attempt at using that function ill-formed in a
way that SFINAE cannot detect.

You can fix your code by replacing the constructor with the invalid default
argument with a pair of constructors:

  Middle(const int &, const T &) {}
  Middle(const int &n) : Middle(n, {}) {}

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/20170528/116a8bac/attachment-0001.html>

More information about the llvm-bugs mailing list