Bug 80990 - cv-qualifiers ignored in variable definition using class template argument deduction
Summary: cv-qualifiers ignored in variable definition using class template argument de...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: patch, wrong-code
: 83818 (view as bug list)
Depends on:
Blocks:
 
Reported: 2017-06-06 16:27 UTC by Jonathan Wakely
Modified: 2021-04-22 19:24 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-06-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2017-06-06 16:27:09 UTC
This valid C++17 program fails to compile:


template<typename T> struct container { };

void must_be_const(const container<int>&) { }
void must_be_const(container<int>&) = delete;

int main()
{
  container<int> c;
  const container d = c;
  must_be_const(d);
}

$ g++ -std=c++17 const.cc
const.cc: In function ‘int main()’:
const.cc:10:18: error: use of deleted function ‘void must_be_const(container<int>&)’
   must_be_const(d);
                  ^
const.cc:4:6: note: declared here
 void must_be_const(container<int>&) = delete;
      ^~~~~~~~~~~~~


The variable 'd' should be const, so should not call the deleted overload.
Comment 1 Jonathan Wakely 2017-06-06 17:06:57 UTC
FWIW using -fconcepts and const container<auto> doesn't lose the qualifiers.
Comment 2 Jonathan Wakely 2017-06-06 17:30:17 UTC
Testing this patch:

--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -25367,7 +25367,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
   --cp_unevaluated_operand;
   release_tree_vector (args);
 
-  return TREE_TYPE (t);
+  return cp_build_qualified_type (TREE_TYPE (t), cp_type_quals (ptype));
 }
 
 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
Comment 3 Jonathan Wakely 2017-06-06 23:52:06 UTC
Patch posted to https://gcc.gnu.org/ml/gcc-patches/2017-06/msg00372.html
Comment 4 Jonathan Wakely 2017-06-07 11:35:08 UTC
Author: redi
Date: Wed Jun  7 11:34:36 2017
New Revision: 248966

URL: https://gcc.gnu.org/viewcvs?rev=248966&root=gcc&view=rev
Log:
PR c++/80990 use cv-qualifiers in class template argument deduction

gcc/cp:

	PR c++/80990
	* pt.c (do_class_deduction): Build qualified type.

gcc/testsuite:

	PR c++/80990
	* g++.dg/cpp1z/class-deduction39.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1z/class-deduction39.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/pt.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Jonathan Wakely 2017-06-07 12:09:56 UTC
Fixed on trunk so far, I'd like to backport this to gcc-7-branch.
Comment 6 Martin Liška 2018-11-20 08:11:11 UTC
Jonathan: Can you please update Known to work?
Comment 7 Jonathan Wakely 2018-12-19 13:55:11 UTC
I no longer plan to backport this.
Comment 8 Patrick Palka 2021-04-22 19:24:41 UTC
*** Bug 83818 has been marked as a duplicate of this bug. ***