 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri Apr 14, 2006 8:06 am Post subject: template argument deduction for reference to const array |
|
|
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 ] |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Fri Apr 14, 2006 4:06 pm Post subject: Re: template argument deduction for reference to const array |
|
|
| Quote: | template<int len
void doit( char const (&buffer)[len] )
{}
int main()
{
char ch[10];
doit( ch );
}
|
I would haveve thought this would compile just fine, but I got an error
when I tried to compile it there using g++.
-Tomás
---
[ 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 ] |
|
| Back to top |
|
 |
Martin Vejnár Guest
|
Posted: Fri Apr 14, 2006 6:06 pm Post subject: Re: template argument deduction for reference to const array |
|
|
rogero (AT) howzatt (DOT) demon.co.uk wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Apr 14, 2006 8:06 pm Post subject: Re: template argument deduction for reference to const array |
|
|
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 ] |
|
| Back to top |
|
 |
Graeme Prentice Guest
|
Posted: Sat Apr 15, 2006 3:06 am Post subject: Re: template argument deduction for reference to const array |
|
|
On Fri, 14 Apr 2006 02:35:39 CST, rogero (AT) howzatt (DOT) demon.co.uk wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Martin Vejnár Guest
|
Posted: Sun Apr 16, 2006 5:06 pm Post subject: Re: template argument deduction for reference to const array |
|
|
Martin Vejnár wrote:
| Quote: | [...] 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 (&)[]")?
|
Ok, I see it now.
[8.3.4/1, note at the end of the paragraph]
..an “array of N cv-qualifier-seq T” has 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.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Bronek Kozicki Guest
|
Posted: Sun Apr 16, 2006 5:06 pm Post subject: Re: template argument deduction for reference to const array |
|
|
rogero (AT) howzatt (DOT) demon.co.uk wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Roger Orr Guest
|
Posted: Sun Apr 16, 2006 7:06 pm Post subject: Re: template argument deduction for reference to const array |
|
|
"Graeme Prentice" <gp.kiwi (AT) gmail (DOT) com> wrote in message
news:pph042pknqqulr25djf25ocl7ltnbujg31 (AT) 4ax (DOT) com...
| Quote: | On Fri, 14 Apr 2006 02:35:39 CST, rogero (AT) howzatt (DOT) 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.
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 ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|