* why this testcase compile failed for gcc.
@ 2011-02-17 7:42 zhang qingshan
2011-02-18 15:08 ` Ian Lance Taylor
0 siblings, 1 reply; 9+ messages in thread
From: zhang qingshan @ 2011-02-17 7:42 UTC (permalink / raw)
To: gcc-help
/* ------- test case -------------------*/
void x();
template <typename T>
void foo(const T*);
int main() {
foo(x);
}
GCC 4.5 complains:
a.cpp: In function 'int main()':
a.cpp:6: error: no matching function for call to 'foo(void (&)())'
It seems that, const T * is resolved as void (&)());
IMO, T --> void (), const T --> void (), const T * --> void (*)(), and
it should be leagle.
Thanks
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-17 7:42 why this testcase compile failed for gcc zhang qingshan
@ 2011-02-18 15:08 ` Ian Lance Taylor
2011-02-18 17:36 ` Marc Glisse
2011-02-21 11:18 ` zhang qingshan
0 siblings, 2 replies; 9+ messages in thread
From: Ian Lance Taylor @ 2011-02-18 15:08 UTC (permalink / raw)
To: zhang qingshan; +Cc: gcc-help
zhang qingshan <steven.zhang54373@gmail.com> writes:
> /* ------- test case -------------------*/
> void x();
> template <typename T>
> void foo(const T*);
>
> int main() {
> foo(x);
> }
>
> GCC 4.5 complains:
>
> a.cpp: In function 'int main()':
> a.cpp:6: error: no matching function for call to 'foo(void (&)())'
>
> It seems that, const T * is resolved as void (&)());
>
> IMO, T --> void (), const T --> void (), const T * --> void (*)(), and
> it should be leagle.
You are confusing function pointers with pointers to data objects. They
are not the same thing in C++. That is, a function pointer is not a
special type of pointer.
Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-18 15:08 ` Ian Lance Taylor
@ 2011-02-18 17:36 ` Marc Glisse
2011-02-21 6:31 ` zhang qingshan
2011-02-21 11:18 ` zhang qingshan
1 sibling, 1 reply; 9+ messages in thread
From: Marc Glisse @ 2011-02-18 17:36 UTC (permalink / raw)
To: gcc-help; +Cc: zhang qingshan
On Fri, 18 Feb 2011, Ian Lance Taylor wrote:
> zhang qingshan <steven.zhang54373@gmail.com> writes:
>
>> /* ------- test case -------------------*/
>> void x();
>> template <typename T>
>> void foo(const T*);
>>
>> int main() {
>> foo(x);
>> }
>>
>> GCC 4.5 complains:
>>
>> a.cpp: In function 'int main()':
>> a.cpp:6: error: no matching function for call to 'foo(void (&)())'
>>
>> It seems that, const T * is resolved as void (&)());
>>
>> IMO, T --> void (), const T --> void (), const T * --> void (*)(), and
>> it should be leagle.
>
> You are confusing function pointers with pointers to data objects. They
> are not the same thing in C++. That is, a function pointer is not a
> special type of pointer.
Well it still kind of is. Without the "const", the code compiles fine. But
the fact that a const qualifier on a function type is ignored seems not to
apply to template type deduction.
(note that most compilers behave like g++ here, except for clang which
accepts the original code)
--
Marc Glisse
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-18 17:36 ` Marc Glisse
@ 2011-02-21 6:31 ` zhang qingshan
0 siblings, 0 replies; 9+ messages in thread
From: zhang qingshan @ 2011-02-21 6:31 UTC (permalink / raw)
To: gcc-help, zhang qingshan; +Cc: Marc Glisse
Thanks, Marc. Please consider this test code:
void f();
template <typename T> void foo(const T);
void Test()
{
foo(f);
}
int main(Test());
It compiles sucessfully for GCC 4.5. As you know, we are trying to add
cv-qualifier on top of function type while template deduce happens.
On Fri, Feb 18, 2011 at 11:38 PM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Fri, 18 Feb 2011, Ian Lance Taylor wrote:
>
>> zhang qingshan <steven.zhang54373@gmail.com> writes:
>>
>>> /* ------- test case -------------------*/
>>> void x();
>>> template <typename T>
>>> void foo(const T*);
>>>
>>> int main() {
>>> foo(x);
>>> }
>>>
>>> GCC 4.5 complains:
>>>
>>> a.cpp: In function 'int main()':
>>> a.cpp:6: error: no matching function for call to 'foo(void (&)())'
>>>
>>> It seems that, const T * is resolved as void (&)());
>>>
>>> IMO, T --> void (), const T --> void (), const T * --> void (*)(), and
>>> it should be leagle.
>>
>> You are confusing function pointers with pointers to data objects. They
>> are not the same thing in C++. That is, a function pointer is not a
>> special type of pointer.
>
> Well it still kind of is. Without the "const", the code compiles fine. But
> the fact that a const qualifier on a function type is ignored seems not to
> apply to template type deduction.
>
> (note that most compilers behave like g++ here, except for clang which
> accepts the original code)
>
> --
> Marc Glisse
>
Consider this test case:
void f();
template <typename T> foo(const T&);
void Test()
{
foo(f);
}
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-18 15:08 ` Ian Lance Taylor
2011-02-18 17:36 ` Marc Glisse
@ 2011-02-21 11:18 ` zhang qingshan
2011-02-21 11:28 ` Jonathan Wakely
2011-02-21 16:00 ` Ian Lance Taylor
1 sibling, 2 replies; 9+ messages in thread
From: zhang qingshan @ 2011-02-21 11:18 UTC (permalink / raw)
To: Ian Lance Taylor; +Cc: gcc-help
IMO, function pointer is a pointer which its type descriptor is a
function type ...
Any difference with the normal pointer?
On Fri, Feb 18, 2011 at 11:00 PM, Ian Lance Taylor <iant@google.com> wrote:
> zhang qingshan <steven.zhang54373@gmail.com> writes:
>
>> /* ------- test case -------------------*/
>> void x();
>> template <typename T>
>> void foo(const T*);
>>
>> int main() {
>> foo(x);
>> }
>>
>> GCC 4.5 complains:
>>
>> a.cpp: In function 'int main()':
>> a.cpp:6: error: no matching function for call to 'foo(void (&)())'
>>
>> It seems that, const T * is resolved as void (&)());
>>
>> IMO, T --> void (), const T --> void (), const T * --> void (*)(), and
>> it should be leagle.
>
> You are confusing function pointers with pointers to data objects. They
> are not the same thing in C++. That is, a function pointer is not a
> special type of pointer.
>
> Ian
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-21 11:18 ` zhang qingshan
@ 2011-02-21 11:28 ` Jonathan Wakely
2011-02-21 16:00 ` Ian Lance Taylor
1 sibling, 0 replies; 9+ messages in thread
From: Jonathan Wakely @ 2011-02-21 11:28 UTC (permalink / raw)
To: zhang qingshan; +Cc: Ian Lance Taylor, gcc-help
On 21 February 2011 06:31, zhang qingshan wrote:
> IMO, function pointer is a pointer which its type descriptor is a
> function type ...
>
> Any difference with the normal pointer?
An lvalue of function type T can be implicitly converted to T*.
That is not true for non-function types.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-21 11:18 ` zhang qingshan
2011-02-21 11:28 ` Jonathan Wakely
@ 2011-02-21 16:00 ` Ian Lance Taylor
2011-02-22 19:10 ` Andi Hellmund
1 sibling, 1 reply; 9+ messages in thread
From: Ian Lance Taylor @ 2011-02-21 16:00 UTC (permalink / raw)
To: zhang qingshan; +Cc: gcc-help
zhang qingshan <steven.zhang54373@gmail.com> writes:
> IMO, function pointer is a pointer which its type descriptor is a
> function type ...
>
> Any difference with the normal pointer?
The C standard does not require that a function pointer use the same
representation as any other type of pointer. In particular, on
processors with Harvard architectures, function pointers and regular
pointers are inherently different.
Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-21 16:00 ` Ian Lance Taylor
@ 2011-02-22 19:10 ` Andi Hellmund
2011-02-22 19:18 ` Jonathan Wakely
0 siblings, 1 reply; 9+ messages in thread
From: Andi Hellmund @ 2011-02-22 19:10 UTC (permalink / raw)
To: Ian Lance Taylor; +Cc: zhang qingshan, gcc-help
Hey
> The C standard does not require that a function pointer use the same
> representation as any other type of pointer. In particular, on
> processors with Harvard architectures, function pointers and regular
> pointers are inherently different.
>
@Ian: do you have a reference section where this is described. I shortly
tried to search for a few keywords in the standard, but couldn't find
the obvious. I would just be interested in the details ...
@Steven: to give you a practical example: HP-UX on Itanium for example
uses two different representations for data pointers and functions
pointers: while data pointers are usual 64-bit addresses (purely
assuming 64-bit compilation mode), function pointers however are
pointers to a data structure called function description which contains
two further pointers: (a) the final target address and (b) a so-called
global data pointer for relative data addressing. Though if you would
print out a function pointer on HP-UX, you would also get a single
address, but it is NOT the address of the function to be called. This
example is not as good as Ian's one, but is somehow similar, I think.
Best regards,
Andi
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: why this testcase compile failed for gcc.
2011-02-22 19:10 ` Andi Hellmund
@ 2011-02-22 19:18 ` Jonathan Wakely
0 siblings, 0 replies; 9+ messages in thread
From: Jonathan Wakely @ 2011-02-22 19:18 UTC (permalink / raw)
To: Andi Hellmund; +Cc: Ian Lance Taylor, zhang qingshan, gcc-help
On 22 February 2011 18:45, Andi Hellmund wrote:
> Hey
>>
>> The C standard does not require that a function pointer use the same
>> representation as any other type of pointer. In particular, on
>> processors with Harvard architectures, function pointers and regular
>> pointers are inherently different.
>>
>
> @Ian: do you have a reference section where this is described. I shortly
> tried to search for a few keywords in the standard, but couldn't find the
> obvious. I would just be interested in the details ...
6.2.5 paragraph 26 covers representation.
6.3.2.3 paragraphs 1, 7 and 8 cover allowed conversions between
pointer types. A pointer to function type cannot be converted to a
pointer to object or incomplete type.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2011-02-22 19:10 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-17 7:42 why this testcase compile failed for gcc zhang qingshan
2011-02-18 15:08 ` Ian Lance Taylor
2011-02-18 17:36 ` Marc Glisse
2011-02-21 6:31 ` zhang qingshan
2011-02-21 11:18 ` zhang qingshan
2011-02-21 11:28 ` Jonathan Wakely
2011-02-21 16:00 ` Ian Lance Taylor
2011-02-22 19:10 ` Andi Hellmund
2011-02-22 19:18 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).