Bug 24663 - Template instantiation generating a zero-sized array doesn't fail
Summary: Template instantiation generating a zero-sized array doesn't fail
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 79957 96944 (view as bug list)
Depends on:
Blocks: 24666 101239
  Show dependency treegraph
 
Reported: 2005-11-04 02:09 UTC by Joshua Conner
Modified: 2021-10-01 10:32 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 2.95.3, 3.2.3, 3.3.3, 3.4.0, 4.0.0, 4.1.0
Last reconfirmed: 2021-08-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joshua Conner 2005-11-04 02:09:29 UTC
The following compliant code fails to compile:

  template<int I> int f1 (char[I]);
  template<int I> int f1 (char p1 = I);
  int i = f1<0>(0);

.. instead it generates an error, e.g.:

  test.C:3: error: call of overloaded 'f1(int)' is ambiguous
  test.C:1: note: candidates are: int f1(char*) [with int I = 0]
  test.C:2: note:                 int f1(char) [with int I = 0]

The first template should fail to match (SFINAE) because doing so would cause a reference to a zero-sized array (as per ISO C++ section 14.8.2.2).

It is possible that gcc may be performing the "type[size] --> type *" conversion too early, as prohibited by 14.8.2.3:

  After this substitution is performed, the function parameter type
  adjustments described in 8.3.5 are performed. [Example: A parameter
  type of “void ()(const int, int[5])” becomes “void(*)(int,int*)”.
Comment 1 Andrew Pinski 2005-11-04 04:44:37 UTC
Confirmed, I think this is another case of the arrays decaying too early.
Comment 2 Andrew Pinski 2005-11-04 04:53:22 UTC
Note you are most likely right that GCC is applying pointers to array too early, see the meta-bug (PR 24666) which I just filed after you filed these two bugs, there were two other bugs about arrays decaying too early to pointers.
Comment 3 Patrick Palka 2016-01-19 00:19:49 UTC
Author: ppalka
Date: Tue Jan 19 00:19:16 2016
New Revision: 232547

URL: https://gcc.gnu.org/viewcvs?rev=232547&root=gcc&view=rev
Log:
Fix the remaining PR c++/24666 blockers (arrays decay to pointers too early)

gcc/cp/ChangeLog:

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* decl.c (grokdeclarator): Don't decay array parameter type to
	a pointer type if it's dependent.
	(grokparms): Invoke strip_top_quals instead of directly invoking
	cp_build_qualified_type.
	* pt.c (decay_dependent_array_parm_type): New static function.
	(type_unification_real): Call decay_dependent_array_parm_type
	to decay a dependent array parameter type to its corresponding
	pointer type before unification.
	(more_specialized_fn): Likewise.
	(get_bindings): Likewise.
	* tree.c (cp_build_qualified_type): Trivial typofix in
	documentation.

gcc/testsuite/ChangeLog:

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* g++.dg/template/pr11858.C: New test.
	* g++.dg/template/pr24663.C: New test.
	* g++.dg/template/unify12.C: New test.
	* g++.dg/template/unify13.C: New test.
	* g++.dg/template/unify14.C: New test.
	* g++.dg/template/unify15.C: New test.
	* g++.dg/template/unify16.C: New test.
	* g++.dg/template/unify17.C: New test.


Added:
    trunk/gcc/testsuite/g++.dg/template/pr11858.C
    trunk/gcc/testsuite/g++.dg/template/pr24663.C
    trunk/gcc/testsuite/g++.dg/template/unify12.C
    trunk/gcc/testsuite/g++.dg/template/unify13.C
    trunk/gcc/testsuite/g++.dg/template/unify14.C
    trunk/gcc/testsuite/g++.dg/template/unify15.C
    trunk/gcc/testsuite/g++.dg/template/unify16.C
    trunk/gcc/testsuite/g++.dg/template/unify17.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Patrick Palka 2016-01-19 16:09:12 UTC
Fixed for GCC 6.
Comment 5 Patrick Palka 2016-01-24 17:45:53 UTC
Author: ppalka
Date: Sun Jan 24 17:45:21 2016
New Revision: 232778

URL: https://gcc.gnu.org/viewcvs?rev=232778&root=gcc&view=rev
Log:
Revert "Fix the remaining PR c++/24666 blockers"

gcc/cp/ChangeLog:

	Revert:
	2016-01-18  Patrick Palka  <ppalka@gcc.gnu.org>

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* decl.c (grokdeclarator): Don't decay array parameter type to
	a pointer type if it's dependent.
	(grokparms): Invoke strip_top_quals instead of directly invoking
	cp_build_qualified_type.
	* pt.c (decay_dependent_array_parm_type): New static function.
	(type_unification_real): Call decay_dependent_array_parm_type
	to decay a dependent array parameter type to its corresponding
	pointer type before unification.
	(more_specialized_fn): Likewise.
	(get_bindings): Likewise.
	* tree.c (cp_build_qualified_type): Trivial typofix in
	documentation.

gcc/testsuite/ChangeLog:

	Revert:
	2016-01-18  Patrick Palka  <ppalka@gcc.gnu.org>

	PR c++/11858
	PR c++/24663
	PR c++/24664
	* g++.dg/template/pr11858.C: New test.
	* g++.dg/template/pr24663.C: New test.
	* g++.dg/template/unify12.C: New test.
	* g++.dg/template/unify13.C: New test.
	* g++.dg/template/unify14.C: New test.
	* g++.dg/template/unify15.C: New test.
	* g++.dg/template/unify16.C: New test.
	* g++.dg/template/unify17.C: New test.


Removed:
    trunk/gcc/testsuite/g++.dg/template/pr11858.C
    trunk/gcc/testsuite/g++.dg/template/pr24663.C
    trunk/gcc/testsuite/g++.dg/template/unify12.C
    trunk/gcc/testsuite/g++.dg/template/unify13.C
    trunk/gcc/testsuite/g++.dg/template/unify14.C
    trunk/gcc/testsuite/g++.dg/template/unify15.C
    trunk/gcc/testsuite/g++.dg/template/unify16.C
    trunk/gcc/testsuite/g++.dg/template/unify17.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Patrick Palka 2016-01-24 17:49:10 UTC
Patch reverted due to numerous issues mentioned in the following thread:

https://gcc.gnu.org/ml/gcc-patches/2016-01/msg01463.html
Comment 7 Andrew Pinski 2021-08-17 04:40:17 UTC
*** Bug 96944 has been marked as a duplicate of this bug. ***
Comment 8 Andrew Pinski 2021-08-17 04:41:56 UTC
*** Bug 79957 has been marked as a duplicate of this bug. ***