template argument deduction for reference to const array

template argument deduction for reference to const array

Post by roger » Sat, 15 Apr 2006 11:35:39


I'm getting inconsistent results using templates with argument type of
'reference to array of const'.

Example:

template<int len>
void doit( char const (&buffer)[len] )
{}

int main()
{
char ch[10];

doit( ch );
}

Should this code compile?

MSVC.Net 2003 says 'yes'
GCC 3.4.4/4.0.2 say 'yes'

GCC 3.2.3 says 'no'
Comeau 4.3.3 beta says 'no'

I can't find a specific reference to this in the core issue list.
It might be related to core issue 450
(Binding a reference to const to a cv-qualified array rvalue)

Can anyone shed any light?

Regards,
Roger Orr
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]
 
 
 

template argument deduction for reference to const array

Post by Martin Vej » Sat, 15 Apr 2006 21:26:13


Is it related only to templates? Should the following compile?

void doit(char const (&)[10]) {}
int main()
{
char ch[10];
doit( ch );
}

AFAICS, "char []" -> "const char []" is not a standard conversion and
should not compile.

It, however, compiles fine with MSVC8 and Comeau, which leads me to
believe, that I've overlooked something. Can someone point me to where
the Standard says that the above is a standard conversion (or that it is
legal to bind "char []" to "const char (&)[]")?

--
Martin

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]

 
 
 

template argument deduction for reference to const array

Post by roger » Sat, 15 Apr 2006 23:44:59

The code compiles successfully if a non-template function is used, and
also if the template argument is specified:

doit<10>( ch );

The problem seems to be to do with template argument deduction.

Regards,
Roger Orr.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]
 
 
 

template argument deduction for reference to const array

Post by NULL » Sun, 16 Apr 2006 00:51:04

> template<int len>


I would haveve thought this would compile just fine, but I got an error
when I tried to compile it there using g++.

-Tom

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]
 
 
 

template argument deduction for reference to const array

Post by gp.kiw » Sun, 16 Apr 2006 11:23:27


This is a bug in Comeau 4.3.3 (EDG) and fixed in the latest Comeau
release. Daveed Vandevoorde described it here
http://www.yqcomputer.com/
which I'm reproducing below for convenience

<<<<<<<<<<, quote Daveede >>>>>>>>>>
OK. Just work your way through 14.8.2.1.

Para 1: Our A is "int[10]" and our P is "T const(&)[S]".
Para 2:
P is a reference type, so skip the 3 bullets.
P is not a cv-qualified type (references never are),
so skip the first sentence after the bullets.
P is a reference type, so we use P' = "T const [S]"
for deduction purposes.
Para 3:
We try to find T, S such that P' becomes identical
to A. That's not possible, so we looks at the
three bullets. The 2nd and 3rd don't apply, but
the first implies that T=int, S=10 is OK because
"int const[10]" is more cv-qualified that "int[10]"
(see 3.9/5).

So deduction succeeds.
<<<<<<<<<<<<< end quote >>>>>>>>>>>>.


However the wording in 14.8.2.1 is being tinkered with in active issue
522
http://www.yqcomputer.com/ #522

where the word transformed is added (the deduced A can be more CV
qualified than the transformed A), but in this case, A and transformed
A are the same.

Note also issue 214 partial ordering, where references are first
removed, then top level CV qualifiers are removed.
http://www.yqcomputer.com/ #214


The standard doesn't define what a "top level" CV qualifier is.
Presumably with int const arr[10]; the const is a top level CV
qualifier. In 5.2.8/5, const&D is implied as having a top level CV
qualifier, which suggests to me that 14.8.2.1/2 would benefit from
pointing out that references are never "CV qualified types" as Daveede
does above.

Graeme

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]
 
 
 

template argument deduction for reference to const array

Post by Roger Or » Mon, 17 Apr 2006 22:37:54


[snip]

Thanks, I missed that thread.


14.8.2.1 is pretty complex ...but I believe the existing wording covers this
case already.
But I alway worry when I disagree with Comeau because it's usually me at
fault.

Regards,
Roger Orr

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]
 
 
 

template argument deduction for reference to const array

Post by avaka » Tue, 18 Apr 2006 01:56:38


Ok, I see it now.

[8.3.4/1, note at the end of the paragraph]
..an rray of N cv-qualifier-seq Thas cv-qualified type...

That makes the 'const' in "const char []" a top-level cv-qualifier.
According to 8.5.3, the "char []" can be bound directly to "const char
(&)[]".

--
Martin

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]
 
 
 

template argument deduction for reference to const array

Post by brok » Tue, 18 Apr 2006 01:58:48


it compiles successfully on Como 4.3.4.1 (beta), which is based on fixed
version of EDG frontend


B.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.yqcomputer.com/ ]