🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.mac.programmer.codewarrior
7 messages
7 total messages Started by Edward Chew Thu, 29 May 2003 20:53
Function pointer casting
#815
Author: Edward Chew
Date: Thu, 29 May 2003 20:53
6 lines
1539 bytes
After upgrading to CW 8, I notice the compiler no longer allows implicit 
casting from function pointers (e.g. "int (*)()") to void*.  Is this a 
bug or some weird C/C++ standards compliance thing?  For now, it seems 
happy if I write static_cast<void*>(fn).

Edward Chew
Re: Function pointer casting
#818
Author: MW Ron
Date: Mon, 02 Jun 2003 12:50
21 lines
1830 bytes
In article <3ED6734A.6080600@lgl.kos.net>,
 Edward Chew <ekc-noreply@lgl.kos.net> wrote:

>After upgrading to CW 8, I notice the compiler no longer allows implicit 
>casting from function pointers (e.g. "int (*)()") to void*.  Is this a 
>bug or some weird C/C++ standards compliance thing?  For now, it seems 
>happy if I write static_cast<void*>(fn).

These type of conversions are illegal, actually even the explicit
conversion is illegal.   However it seems to be common practice to do 
this so we allowed the explicit casts for legacy code.

Ron

-- 
  Get printed CodeWarrior Documentation (limited selection) 
     And all the latest versions of CodeWarrior Manuals

http://www.metrowerks.com/MW/Support/dev_resources/default.htm

Ron Liechty - MWRon@metrowerks.com - http://www.metrowerks.com
Re: Function pointer casting
#819
Author: MW Ron
Date: Mon, 02 Jun 2003 13:41
20 lines
1730 bytes
In article <3ED6734A.6080600@lgl.kos.net>,
 Edward Chew <ekc-noreply@lgl.kos.net> wrote:

>After upgrading to CW 8, I notice the compiler no longer allows implicit 
>casting from function pointers (e.g. "int (*)()") to void*.  Is this a 
>bug or some weird C/C++ standards compliance thing?  For now, it seems 
>happy if I write static_cast<void*>(fn).

This is not legal implicitly or explicitly but we allow the explicit 
cast because this is common for legacy code.  

Ron

-- 
  Get printed CodeWarrior Documentation (limited selection) 
     And all the latest versions of CodeWarrior Manuals

http://www.metrowerks.com/MW/Support/dev_resources/default.htm

Ron Liechty - MWRon@metrowerks.com - http://www.metrowerks.com
Re: Function pointer casting
#820
Author: Edward Chew
Date: Mon, 02 Jun 2003 15:20
34 lines
2671 bytes
MW Ron wrote:
> In article <3ED6734A.6080600@lgl.kos.net>,
>  Edward Chew <ekc-noreply@lgl.kos.net> wrote:
> 
> 
>>After upgrading to CW 8, I notice the compiler no longer allows implicit 
>>casting from function pointers (e.g. "int (*)()") to void*.  Is this a 
>>bug or some weird C/C++ standards compliance thing?  For now, it seems 
>>happy if I write static_cast<void*>(fn).
> 
> 
> This is not legal implicitly or explicitly but we allow the explicit 
> cast because this is common for legacy code.  

Interesting.  I wonder why it would be illegal, and what the legal 
alternative would be?  I ran into this problem when weak-linking to 
certain CFM libraries and testing to see if they are available.  In 
Apple literature, they recommend testing the init routine like this:

     if(LibraryInitFunction == kUnresolvedCFragSymbolAddress)
         reportLibraryUnavailable();

This will immediately cause a type mismatch error at compile time in 
Codewarrior 8.  Figuring a pointer is a pointer, I rewrote this as:

     if(static_cast<void*>(LibraryInitFunction) ==
        static_cast<void*>(kUnresolvedCFragSymbolAddress)
        )
          reportLibraryUnavailable();

This seems to work fine, but now I am told it is illegal as well.  What 
would be the proper test in that case?

Edward K. Chew
Re: Function pointer casting
#821
Author: MW Ron
Date: Mon, 02 Jun 2003 16:01
48 lines
2867 bytes
In article <3EDB6B20.2000804@lgl.kos.net>,
 Edward Chew <ekc-noreply@lgl.kos.net> wrote:

>MW Ron wrote:
>> In article <3ED6734A.6080600@lgl.kos.net>,
>>  Edward Chew <ekc-noreply@lgl.kos.net> wrote:
>> 
>> 
>>>After upgrading to CW 8, I notice the compiler no longer allows implicit 
>>>casting from function pointers (e.g. "int (*)()") to void*.  Is this a 
>>>bug or some weird C/C++ standards compliance thing?  For now, it seems 
>>>happy if I write static_cast<void*>(fn).
>> 
>> 
>> This is not legal implicitly or explicitly but we allow the explicit 
>> cast because this is common for legacy code.  
>
>Interesting.  I wonder why it would be illegal, and what the legal 
>alternative would be?  I ran into this problem when weak-linking to 
>certain CFM libraries and testing to see if they are available.  In 
>Apple literature, they recommend testing the init routine like this:
>
>     if(LibraryInitFunction == kUnresolvedCFragSymbolAddress)
>         reportLibraryUnavailable();
>
>This will immediately cause a type mismatch error at compile time in 
>Codewarrior 8.  Figuring a pointer is a pointer, I rewrote this as:
>
>     if(static_cast<void*>(LibraryInitFunction) ==
>        static_cast<void*>(kUnresolvedCFragSymbolAddress)
>        )
>          reportLibraryUnavailable();
>
>This seems to work fine, but now I am told it is illegal as well.  What 
>would be the proper test in that case?

You might still get away with this in C, and relaxed pointer rules did 
you try it?  

Ron

-- 
  Get printed CodeWarrior Documentation (limited selection) 
     And all the latest versions of CodeWarrior Manuals

http://www.metrowerks.com/MW/Support/dev_resources/default.htm

Ron Liechty - MWRon@metrowerks.com - http://www.metrowerks.com
Re: Function pointer casting
#822
Author: David Phillip Oster
Date: Mon, 02 Jun 2003 16:24
28 lines
2907 bytes
In article <3EDB6B20.2000804@lgl.kos.net>,
 Edward Chew <ekc-noreply@lgl.kos.net> wrote:

>      if(static_cast<void*>(LibraryInitFunction) ==
>         static_cast<void*>(kUnresolvedCFragSymbolAddress)
>         )
>           reportLibraryUnavailable();
> 
> This seems to work fine, but now I am told it is illegal as well.  What 
> would be the proper test in that case?

#define kUnresolvedCFragSymbolAddress \
      static_cast<int>(kUnresolvedCFragSymbolAddress)

      if(LibraryInitFunction == kUnresolvedCFragSymbolAddress){
      }

Discussion: It is always legal to compare a pointer of any type to zero.
(int Zero is special this way. (Long zero ( 0L ) is not.))
In this context, a bare zero would be misleading. We give zero a 
symbolic name, like NULL and noErr are symbolic names for zero, and you
use the correct one, depending on a context, as an aid to those who
will read your source code in the future.

Apple mistakenly defined kUnresolvedCFragSymbolAddress as 0 in an enum,
rather than a #define, which is OK for C but not for C++. The #define
definition overrides the enum definition. A bug report with Apple should
be filed on this.
Re: Function pointer casting
#823
Author: Seebs
Date: Mon, 02 Jun 2003 18:50
29 lines
2081 bytes
In article <3EDB6B20.2000804@lgl.kos.net>,
Edward Chew  <ekc-noreply@lgl.kos.net> wrote:
>Interesting.  I wonder why it would be illegal, and what the legal 
>alternative would be?

Can't speak for C++, but in C:
* It's illegal because function pointers may be "bigger" than object pointers.
* The legal alternative is to pick an arbitrary "neutral" function pointer
  type, and cast everything to and from that, because the language promises
  that appropriately-cast function pointers are still magic.

So,
	typedef void (*func_p)(void);
	typedef int (*printf_t)(const restrict char *, ...);

	func_p x = (func_p) printf;
	int i;
	i = *((printf_t) x)("hello, world!");

should work.

(I could have the casts wrong, I almost never need to do this.)

-s
-- 
   Copyright 2003, all wrongs reversed.  Peter Seebach / seebs@plethora.net
   http://www.seebs.net/log/ - YA blog.   http://www.seebs.net/ - homepage.
     C/Unix wizard, pro-commerce radical, spam fighter.  Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads