Topic: template argument deduction for reference to const array
Author: rogero@howzatt.demon.co.uk
Date: Fri, 14 Apr 2006 02:35:39 CST Raw View
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
--
MVP in C++ at www.brainbench.com
Roger.
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: NULL@NULL.NULL ("Tom s")
Date: Fri, 14 Apr 2006 15:51:04 GMT Raw View
> template<int len>
> void doit( char const (&buffer)[len] )
> {}
>=20
> int main()
> {
> char ch[10];
>=20
> doit( ch );
> }
I would haveve thought this would compile just fine, but I got an error=20
when I tried to compile it there using g++.
-Tom=E1s
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Martin_Vejn=E1r?= <avakar@volny.cz>
Date: Fri, 14 Apr 2006 12:26:13 CST Raw View
rogero@howzatt.demon.co.uk wrote:
> 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?
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.comeaucomputing.com/csc/faq.html ]
Author: rogero@howzatt.demon.co.uk
Date: Fri, 14 Apr 2006 14:44:59 CST Raw View
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.
--
MVP in C++ at www.brainbench.com
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: gp.kiwi@gmail.com (Graeme Prentice)
Date: Sat, 15 Apr 2006 02:23:27 GMT Raw View
On Fri, 14 Apr 2006 02:35:39 CST, rogero@howzatt.demon.co.uk wrote:
>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?
This is a bug in Comeau 4.3.3 (EDG) and fixed in the latest Comeau
release. Daveed Vandevoorde described it here
http://makeashorterlink.com/?U1C8216FC
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.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#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.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#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.comeaucomputing.com/csc/faq.html ]
Author: avakar@volny.cz (=?windows-1252?Q?Martin_Vejn=E1r?=)
Date: Sun, 16 Apr 2006 16:56:38 GMT Raw View
Martin Vejn=E1r wrote:
> [...] Should the following compile?
>=20
> void doit(char const (&)[10]) {}
> int main()
> {
> char ch[10];
> doit( ch );
> }
>=20
> AFAICS, "char []" -> "const char []" is not a standard conversion and=20
> should not compile.
>=20
> It, however, compiles fine with MSVC8 and Comeau, which leads me to=20
> believe, that I've overlooked something. Can someone point me to where=20
> the Standard says that the above is a standard conversion (or that it i=
s=20
> legal to bind "char []" to "const char (&)[]")?
>=20
Ok, I see it now.
[8.3.4/1, note at the end of the paragraph]
.an =93array of N cv-qualifier-seq T=94 has cv-qualified type...
That makes the 'const' in "const char []" a top-level cv-qualifier.=20
According to 8.5.3, the "char []" can be bound directly to "const char=20
(&)[]".
--=20
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.comeaucomputing.com/csc/faq.html ]
Author: brok@rubikon.pl (Bronek Kozicki)
Date: Sun, 16 Apr 2006 16:58:48 GMT Raw View
rogero@howzatt.demon.co.uk wrote:
> Comeau 4.3.3 beta says 'no'
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.comeaucomputing.com/csc/faq.html ]
Author: "Roger Orr" <rogero@howzatt.demon.co.uk>
Date: Sun, 16 Apr 2006 13:37:54 CST Raw View
"Graeme Prentice" <gp.kiwi@gmail.com> wrote in message
news:pph042pknqqulr25djf25ocl7ltnbujg31@4ax.com...
> On Fri, 14 Apr 2006 02:35:39 CST, rogero@howzatt.demon.co.uk wrote:
[snip]
> This is a bug in Comeau 4.3.3 (EDG) and fixed in the latest Comeau
> release. Daveed Vandevoorde described it here
> http://makeashorterlink.com/?U1C8216FC
> which I'm reproducing below for convenience
Thanks, I missed that thread.
> However the wording in 14.8.2.1 is being tinkered with in active issue
> 522
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#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.
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
--
MVP in C++ at www.brainbench.com
---
[ 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.comeaucomputing.com/csc/faq.html ]