C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error with template code
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Hendrik Schober
Guest





PostPosted: Mon Apr 04, 2005 1:23 pm    Post subject: Error with template code Reply with quote



Hi,

the following code causes Comeau Online to emit

"ComeauTest.c", line 24: error: no instance of function template "f" matches the
argument list
The argument types that you used are: (int [2])
f(array);
^

"ComeauTest.c", line 25: error: no instance of function template
"Test<T>::f [with T=int]" matches the argument list
The argument types that you used are: (int [2])
object type is: Test<int>
test.f(array);
^

2 errors detected in the compilation of "ComeauTest.c".

Could someone explain to me, why?

TIA,

Schobi


#include <iostream>
#include <cstdlib>

template< typename T, std::size_t N >
int f(const T (&array)[N])
{
std::cout << N << 'n';
return N;
}

template< typename T >
struct Test {
template< std::size_t N >
int f(const T (&array)[N])
{
std::cout << N << 'n';
return N;
}
};

int main()
{ int array[] = { 42, 4711 };
Test f(array);
test.f(array);
return 0;
}


--
[email]SpamTrap (AT) gmx (DOT) de[/email] is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
David Abrahams
Guest





PostPosted: Mon Apr 04, 2005 4:08 pm    Post subject: Re: Error with template code Reply with quote



"Hendrik Schober" <SpamTrap (AT) gmx (DOT) de> writes:

Quote:
Hi,

the following code causes Comeau Online to emit

<snip>

Quote:
2 errors detected in the compilation of "ComeauTest.c".

Could someone explain to me, why?

Wow, it looks like a compiler bug to me. I've reported it to EDG;
hopefully they'll respond here.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Mon Apr 04, 2005 4:12 pm    Post subject: Re: Error with template code Reply with quote



Hendrik Schober wrote:
Quote:
the following code causes Comeau Online to emit

"ComeauTest.c", line 24: error: no instance of function template "f" matches the
argument list
The argument types that you used are: (int [2])
f(array);
^

"ComeauTest.c", line 25: error: no instance of function template
"Test<T>::f [with T=int]" matches the argument list
The argument types that you used are: (int [2])
object type is: Test<int
test.f(array);
^

2 errors detected in the compilation of "ComeauTest.c".

Could someone explain to me, why?

Your function takes a reference to an array of const int. You pass it
a reference to an array of int. The two are incompatible (there is no
implicit conversion). If you declare your 'array' array in 'main' as
an array of const int, the code compiles just fine.

Quote:

TIA,

Schobi


#include #include
template< typename T, std::size_t N
int f(const T (&array)[N])
{
std::cout << N << 'n';
return N;
}

template< typename T
struct Test {
template< std::size_t N
int f(const T (&array)[N])
{
std::cout << N << 'n';
return N;
}
};

int main()
{ int array[] = { 42, 4711 };
Test f(array);
test.f(array);
return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hyman Rosen
Guest





PostPosted: Mon Apr 04, 2005 4:14 pm    Post subject: Re: Error with template code Reply with quote

Hendrik Schober wrote:
Quote:
Could someone explain to me, why?

Template argument matching is exact. Get rid of
the consts and you'll be OK.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Chris Uzdavinis
Guest





PostPosted: Mon Apr 04, 2005 4:14 pm    Post subject: Re: Error with template code Reply with quote

Quote:
"Hendrik Schober" <SpamT... (AT) gmx (DOT) de> writes:
....
template< typename T, std::size_t N
int f(const T (&array)[N])
{
std::cout << N << 'n';
return N;

}

The function expects an array of const integers. If you change the
code in main to this:

const int array[] = { 42, 4711 };

Then it'll match. In Steve Dewhurst's book "C++ Gotchas", I think this
is what he calls the case of the migrating type qualifiers.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Abrahams
Guest





PostPosted: Tue Apr 05, 2005 7:40 am    Post subject: Re: Error with template code Reply with quote

David Abrahams <dave (AT) boost-consulting (DOT) com> writes:

Quote:
"Hendrik Schober" <SpamTrap (AT) gmx (DOT) de> writes:

Hi,

the following code causes Comeau Online to emit

snip

2 errors detected in the compilation of "ComeauTest.c".

Could someone explain to me, why?

Wow, it looks like a compiler bug to me. I've reported it to EDG;
hopefully they'll respond here.

EDG confirms it's a bug, and it's fixed in the latest version of their
front-end.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
lulu yao
Guest





PostPosted: Wed Apr 06, 2005 8:50 pm    Post subject: Re: Error with template code Reply with quote

It's a bug.

Surely, template deduction attempts to find a substitution of the
function template parameters that make the parameterized type identical
to the argument type. However when this is not possible, some argument
conversions are allowable, here is one case. For the parameter is
declared with a reference declarator, it can be more const/volatile
qualified than the argument. Also this case applies to pointer.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Hendrik Schober
Guest





PostPosted: Fri Apr 08, 2005 7:40 am    Post subject: Re: Error with template code Reply with quote

Hendrik Schober <SpamTrap (AT) gmx (DOT) de> wrote:
Quote:
[...]

How was that sig of Anders Munch:
"Still confused but at a higher level."
This applies very well to me here. :)

I suspected this to be a bug in CW9, but
a test with Comeau suggested that I was
wrong. As long as Comeau rejects any code,
other compiler vendors won't accept it as
proof for a bug in their compilers. OTOH,
if the code is indeed wrong, I need to
understand why.
I have certainly bound non-const objects
to const args before, even is those args'
types where template args. What is it,
that supposedly makes this case special?
Or is EDG right that they're wrong? In
general, I'd trust their expertise.

Thank everyone for the help so far.

Schobi

--
[email]SpamTrap (AT) gmx (DOT) de[/email] is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Fri Apr 08, 2005 11:09 pm    Post subject: Re: Error with template code Reply with quote

Hendrik Schober wrote:
Quote:
[...]
I have certainly bound non-const objects
to const args before, even is those args'
types where template args. What is it,
that supposedly makes this case special?
Or is EDG right that they're wrong? In
general, I'd trust their expertise.

I think there is a difference between const objects and arrays of
const objects. With objects, the "top-level" cv qualifier is dropped
with types are matched. But with arrays of objects, I don't think it
is allowed. Also, I am fairly certain that cv qualifiers do not apply
to arrays, only to elements of arrays.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hendrik Schober
Guest





PostPosted: Mon Apr 11, 2005 3:18 pm    Post subject: Re: Error with template code Reply with quote

Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:
Quote:
Hendrik Schober wrote:
[...]
I have certainly bound non-const objects
to const args before, even is those args'
types where template args. What is it,
that supposedly makes this case special?
Or is EDG right that they're wrong? In
general, I'd trust their expertise.

I think there is a difference between const objects and arrays of
const objects. [...]

Wait! Are you saying that 'const T (&array)[N]'
isn't a const array of objects, but an array
of const objects? This wouldn't be what I
wanted to say.

Quote:
V

Schobi

--
[email]SpamTrap (AT) gmx (DOT) de[/email] is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Mon Apr 11, 2005 11:29 pm    Post subject: Re: Error with template code Reply with quote

Victor Bazarov wrote:
Quote:
I think there is a difference between const objects and arrays of
const objects. With objects, the "top-level" cv qualifier is dropped
with types are matched. But with arrays of objects, I don't think it
is allowed.

What does this mean?

Suppose this was true. What could you do with a const array that could
not be done with an array of const objects, or vice-versa?

Perhaps you're thinking of the difference between a const pointer, and
a pointer to const? But an array is not automatically the same thing
as a pointer in all contexts. Do you suggest that a const array would
decay into a const pointer, but an array of const would decay into a
pointer to const?

Would you please tell me where in the standard it says such a thing?
Or else -- at least an example of code that demonstrates the
difference?

Quote:
Also, I am fairly certain that cv qualifiers do not apply
to arrays, only to elements of arrays.

Doesn't this refute what you just said above?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Mon Apr 11, 2005 11:34 pm    Post subject: Re: Error with template code Reply with quote

Hendrik Schober wrote:
Quote:
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote:

Hendrik Schober wrote:

[...]

I have certainly bound non-const objects
to const args before, even is those args'
types where template args. What is it,
that supposedly makes this case special?
Or is EDG right that they're wrong? In
general, I'd trust their expertise.

I think there is a difference between const objects and arrays of
const objects. [...]


Wait! Are you saying that 'const T (&array)[N]'
isn't a const array of objects, but an array
of const objects? This wouldn't be what I
wanted to say.

It's [a reference to] an array of N constant objects of type T.
I don't think that 'const' or 'volatile' applies to arrays at all.
3.9.3/2 says "A compound type (3.9.2) is not cv-qualified by the
cv-qualifiers (if any) of the types from which it is compounded. Any
cv-qualifiers applied to an array type affect the array element type,
not the array type (8.3.4)."

I am not sure how it helps you, though.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Tue Apr 12, 2005 8:50 pm    Post subject: Re: Error with template code Reply with quote

Allan W wrote:
Quote:
Victor Bazarov wrote:

I think there is a difference between const objects and arrays of
const objects. With objects, the "top-level" cv qualifier is dropped
with types are matched. But with arrays of objects, I don't think it
is allowed.


What does this mean?

It means when you write

const T blah[42]

and 'blah' is used somewhere and does not decay to a pointer (there
are context when that's so), 'const' cannot be dropped.

Quote:
Suppose this was true. What could you do with a const array that could
not be done with an array of const objects, or vice-versa?

Perhaps you're thinking of the difference between a const pointer, and
a pointer to const? But an array is not automatically the same thing
as a pointer in all contexts. Do you suggest that a const array would
decay into a const pointer, but an array of const would decay into a
pointer to const?

Would you please tell me where in the standard it says such a thing?
Or else -- at least an example of code that demonstrates the
difference?


Also, I am fairly certain that cv qualifiers do not apply
to arrays, only to elements of arrays.


Doesn't this refute what you just said above?

No. Arrays are not objects. I never claimed there could be const arrays
compared to const elements of an array. It's all your imagination.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Wed Apr 13, 2005 6:44 am    Post subject: Re: Error with template code Reply with quote

Quote:
Victor Bazarov wrote:
I think there is a difference between const objects and arrays of
const objects. With objects, the "top-level" cv qualifier is
dropped
with types are matched. But with arrays of objects, I don't think
it
is allowed.

Allan W wrote:
What does this mean?

Victor Bazarov wrote:
Quote:
It means when you write
const T blah[42]

and 'blah' is used somewhere and does not decay to a pointer (there
are context when that's so), 'const' cannot be dropped.

Are there other situations where const CAN be dropped?

Quote:
Also, I am fairly certain that cv qualifiers do not apply
to arrays, only to elements of arrays.

Doesn't this refute what you just said above?

No. Arrays are not objects. I never claimed there could be const
arrays
compared to const elements of an array. It's all your imagination.

Okay, but I'm finding new depths to my imagination. I might even be
hallucinating, I'm not sure. :-(

In
const int foo[42] = {0};
doesn't this mean that all 42 ints are const?
Isn't this the same thing as saying that the array foo[] is const?
If we agree on this (and I think we do), then I must apologize for
hallucinating so much that I'm missing what point you're trying to make.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Clark S. Cox III
Guest





PostPosted: Wed Apr 13, 2005 8:20 pm    Post subject: Re: Error with template code Reply with quote

On 2005-04-13 02:44:45 -0400, "Allan W" <allan_w (AT) my-dejanews (DOT) com> said:
Quote:

Okay, but I'm finding new depths to my imagination. I might even be
hallucinating, I'm not sure. :-(

In
const int foo[42] = {0};
doesn't this mean that all 42 ints are const?

Yes

Quote:
Isn't this the same thing as saying that the array foo[] is const?

No, foo is an array of const int, *not* a const array of int. For the
same reason that, in the following:

const int i = 0;
const int *bar = &i;
const int &baz = i;

bar is a pointer to const int, *not* a const pointer to int, and baz is
a reference to a const int, *not* a const reference to int. In all of
the above cases, it's "int" that is cv qualified, and in the case of
arrays and references, there is no way to have it otherwise.

Quote:
If we agree on this (and I think we do), then I must apologize for
hallucinating so much that I'm missing what point you're trying to make.


--
Clark S. Cox, III
[email]clarkcox3 (AT) gmail (DOT) com[/email]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.