re PR c++/85883 (class template argument deduction fails in new-expression)
authorMarek Polacek <polacek@redhat.com>
Tue, 29 May 2018 17:44:07 +0000 (17:44 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Tue, 29 May 2018 17:44:07 +0000 (17:44 +0000)
PR c++/85883
* init.c (build_new): Handle deducing a class with new
with more than one argument.

* g++.dg/cpp1z/class-deduction55.C: New test.
* g++.dg/cpp1z/class-deduction56.C: New test.
* g++.dg/cpp1z/class-deduction57.C: New test.

From-SVN: r260901

gcc/cp/ChangeLog
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1z/class-deduction55.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/class-deduction56.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/class-deduction57.C [new file with mode: 0644]

index d411be5d9d8c1038dca4b3911f09e17595426b6e..9ccdfbe594f76a3177b0467c8ccdb0dc2d94314e 100644 (file)
@@ -1,3 +1,9 @@
+2018-05-29  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/85883
+       * init.c (build_new): Handle deducing a class with new
+       with more than one argument.
+
 2018-05-29  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/85952
index de1bfee54bb1fb5e98a98d86dd735ccf12aed26c..b925e843432e783fd3efcbbc9a42f7440d8aabd7 100644 (file)
@@ -3586,11 +3586,27 @@ build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
       if (auto_node)
        {
          tree d_init = NULL_TREE;
-         if (vec_safe_length (*init) == 1)
+         const size_t len = vec_safe_length (*init);
+         /* E.g. new auto(x) must have exactly one element, or
+            a {} initializer will have one element.  */
+         if (len == 1)
            {
              d_init = (**init)[0];
              d_init = resolve_nondeduced_context (d_init, complain);
            }
+         /* For the rest, e.g. new A(1, 2, 3), create a list.  */
+         else if (len > 1)
+           {
+             unsigned int n;
+             tree t;
+             tree *pp = &d_init;
+             FOR_EACH_VEC_ELT (**init, n, t)
+               {
+                 t = resolve_nondeduced_context (t, complain);
+                 *pp = build_tree_list (NULL_TREE, t);
+                 pp = &TREE_CHAIN (*pp);
+               }
+           }
          type = do_auto_deduction (type, d_init, auto_node, complain);
        }
     }
index d71991fb646b969a5753854d5f767accb570b900..83f16eec480c3635f34723e0c8d297b037902439 100644 (file)
@@ -1,3 +1,10 @@
+2018-05-29  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/85883
+       * g++.dg/cpp1z/class-deduction55.C: New test.
+       * g++.dg/cpp1z/class-deduction56.C: New test.
+       * g++.dg/cpp1z/class-deduction57.C: New test.
+
 2018-05-29  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/85952
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction55.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction55.C
new file mode 100644 (file)
index 0000000..a93d720
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/85883
+// { dg-options -std=c++17 }
+
+template <typename T>
+struct Bar
+{
+  Bar(T) { }
+};
+
+int
+main ()
+{
+  auto x = Bar(1);
+  auto y = new Bar(3);
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction56.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction56.C
new file mode 100644 (file)
index 0000000..71dbfa1
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/85883
+// { dg-options -std=c++17 }
+
+template <typename T1, typename T2>
+struct Bar
+{
+  Bar(T1, T2) { }
+};
+
+int
+main ()
+{
+  auto x = Bar(1, 2);
+  auto y = new Bar(3, 4);
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction57.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction57.C
new file mode 100644 (file)
index 0000000..200ba6c
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/85883
+// { dg-options -std=c++17 }
+
+template <typename T1, typename T2, typename T3>
+struct Bar
+{
+  Bar(T1, T2, T3) { }
+};
+
+int
+main ()
+{
+  auto x = Bar(1, 2, 3);
+  auto y = new Bar(3, 4, 5);
+}