You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Christian Gross <Ch...@yahoo.de> on 2001/05/26 20:33:06 UTC

[PATCH] Type conversion needed for C++

I was playing around with hooks and noticed that there is a problem
with using hooks on C++.  The problem was that C++ does not allow a
type conversion without a type cast.  Following is the fix

---------------------------------------------------------------
--- c:/httpd2_16/srclib/apr-util/include/apr_hooks.h	Wed Apr  4
01:35:46 2001
+++ c:/httpd/srclib/apr-util/include/apr_hooks.h	Sat May 26
14:21:58 2001
@@ -98,7 +98,7 @@

_hooks.link_##name=apr_array_make(apr_global_hook_pool,1,sizeof(ns##_LINK_##name##_t));
\
 	apr_hook_sort_register(#name,&_hooks.link_##name); \
 	} \
-    pHook=apr_array_push(_hooks.link_##name); \
+    pHook=(ns##_LINK_##name##_t *)apr_array_push(_hooks.link_##name);
\
     pHook->pFunc=pf; \
     pHook->aszPredecessors=aszPre; \
     pHook->aszSuccessors=aszSucc; \



Re: [PATCH] Type conversion needed for C++

Posted by Christian Gross <Ch...@yahoo.de>.
On Sun, 27 May 2001 00:12:33 -0700, you wrote:

>Adding the cast just doesn't seem right. We implicitly cast void* to other
>things all the time. Why does *this* one break things? I just don't buy the
>need to do the cast.
>
I needed to cast, so that the C++ compiler would not barf on the
compilation.  Consider the following example.

void *func() {
	return (void *)NULL;
}

struct val {
	long something;
};

int main(int argc, char* argv[])
{
	struct val *tst;
	
	// Does not work
	tst = func();

	// Does work
	tst = (struct val *)func();
	return 0;
}

The C++ compiler will not accept the first cast.  In the
APR_IMPLEMENT_xxx macro there is a type cast from a void *
apr_array_make function to a known data type *pHook.  My C++ compiler
actually tells me to make the explicit cast.

Now why does the C++ compiler barf in cast scenario and not anywhere
else?  I was writing some basic hook code and it was written in a C++
program.  The APR_HOOK_xxx macros are being expanded in C++ code and
not C code.  Hence the APR_HOOK_xxx macros have to abide by C++
programming rules.

>Really... void* should be automatically castable to anything.
>
Maybe in C, but not C++.  As per the above example in C++ the cast
will not work.

>I'm guessing something more subtle is occurring, and the cast is simply
>hiding that subtlety.
>
I found the bug and then when I ran cpp to expand the macros found the
actual problematic line.

Christian

>Cheers,
>-g
>
>On Sat, May 26, 2001 at 06:12:38PM -0400, Christian Gross wrote:
>> On Sat, 26 May 2001 14:19:25 -0500, you wrote:
>> 
>> >I believe this would break type saftey.  These are very carefully constructed to
>> >ensure that the proper hook fn is registered for the appropriate hook.
>> >
>> >I'll take a look at your patch later today and see what (if) it breaks anything,
>> >or if we were simply missing the "C" namespace wrappers.
>> >
>> The problem is that apr_array_push returns a void pointer.  And since
>> pHook is a predefined data type there is a type problem.  The extern
>> "C" wrappers only make the function behave using C linkage.  The code
>> within is still treated as C++.  I tried it and the same type cast
>> error still occured.
>> 
>> I think the only way to solve is this to use a type cast.  But correct
>> me if I am wrong.
>> 
>> Christian
>> 
>> >----- Original Message ----- 
>> >From: "Christian Gross" <Ch...@yahoo.de>
>> >Sent: Saturday, May 26, 2001 1:33 PM
>> >
>> >
>> >I was playing around with hooks and noticed that there is a problem
>> >with using hooks on C++.  The problem was that C++ does not allow a
>> >type conversion without a type cast.  Following is the fix
>> >
>> >---------------------------------------------------------------
>> >--- c:/httpd2_16/srclib/apr-util/include/apr_hooks.h Wed Apr  4
>> >01:35:46 2001
>> >+++ c:/httpd/srclib/apr-util/include/apr_hooks.h Sat May 26
>> >14:21:58 2001
>> >@@ -98,7 +98,7 @@
>> >
>> >_hooks.link_##name=apr_array_make(apr_global_hook_pool,1,sizeof(ns##_LINK_##name##_t));
>> >\
>> >  apr_hook_sort_register(#name,&_hooks.link_##name); \
>> >  } \
>> >-    pHook=apr_array_push(_hooks.link_##name); \
>> >+    pHook=(ns##_LINK_##name##_t *)apr_array_push(_hooks.link_##name);
>> >\
>> >     pHook->pFunc=pf; \
>> >     pHook->aszPredecessors=aszPre; \
>> >     pHook->aszSuccessors=aszSucc; \
>> >
>> >
>> >



Re: [PATCH] Type conversion needed for C++

Posted by Branko Čibej <br...@xbc.nu>.
Greg Stein wrote:

>Adding the cast just doesn't seem right. We implicitly cast void* to other
>things all the time. Why does *this* one break things? I just don't buy the
>need to do the cast.
>
>Really... void* should be automatically castable to anything.
>
In C, yes. In C++, implicit casting from void* is not allowed. you need 
a static_cast (or dynamic_cast, depending on context).

-- 
Brane Čibej
    home:   <br...@xbc.nu>             http://www.xbc.nu/brane/
    work:   <br...@hermes.si>   http://www.hermes-softlab.com/
     ACM:   <br...@acm.org>            http://www.acm.org/




Re: [PATCH] Type conversion needed for C++

Posted by Greg Marr <gr...@alum.wpi.edu>.
On Sun, 27 May 2001 00:12:33 -0700
 Greg Stein <gs...@lyra.org> wrote:
> Really... void* should be automatically castable to anything.

In C++, void * isn't automatically castable to anything at all.

Re: [PATCH] Type conversion needed for C++

Posted by Greg Stein <gs...@lyra.org>.
Adding the cast just doesn't seem right. We implicitly cast void* to other
things all the time. Why does *this* one break things? I just don't buy the
need to do the cast.

Really... void* should be automatically castable to anything.

I'm guessing something more subtle is occurring, and the cast is simply
hiding that subtlety.

Cheers,
-g

On Sat, May 26, 2001 at 06:12:38PM -0400, Christian Gross wrote:
> On Sat, 26 May 2001 14:19:25 -0500, you wrote:
> 
> >I believe this would break type saftey.  These are very carefully constructed to
> >ensure that the proper hook fn is registered for the appropriate hook.
> >
> >I'll take a look at your patch later today and see what (if) it breaks anything,
> >or if we were simply missing the "C" namespace wrappers.
> >
> The problem is that apr_array_push returns a void pointer.  And since
> pHook is a predefined data type there is a type problem.  The extern
> "C" wrappers only make the function behave using C linkage.  The code
> within is still treated as C++.  I tried it and the same type cast
> error still occured.
> 
> I think the only way to solve is this to use a type cast.  But correct
> me if I am wrong.
> 
> Christian
> 
> >----- Original Message ----- 
> >From: "Christian Gross" <Ch...@yahoo.de>
> >Sent: Saturday, May 26, 2001 1:33 PM
> >
> >
> >I was playing around with hooks and noticed that there is a problem
> >with using hooks on C++.  The problem was that C++ does not allow a
> >type conversion without a type cast.  Following is the fix
> >
> >---------------------------------------------------------------
> >--- c:/httpd2_16/srclib/apr-util/include/apr_hooks.h Wed Apr  4
> >01:35:46 2001
> >+++ c:/httpd/srclib/apr-util/include/apr_hooks.h Sat May 26
> >14:21:58 2001
> >@@ -98,7 +98,7 @@
> >
> >_hooks.link_##name=apr_array_make(apr_global_hook_pool,1,sizeof(ns##_LINK_##name##_t));
> >\
> >  apr_hook_sort_register(#name,&_hooks.link_##name); \
> >  } \
> >-    pHook=apr_array_push(_hooks.link_##name); \
> >+    pHook=(ns##_LINK_##name##_t *)apr_array_push(_hooks.link_##name);
> >\
> >     pHook->pFunc=pf; \
> >     pHook->aszPredecessors=aszPre; \
> >     pHook->aszSuccessors=aszSucc; \
> >
> >
> >

-- 
Greg Stein, http://www.lyra.org/

Re: [PATCH] Type conversion needed for C++

Posted by Christian Gross <Ch...@yahoo.de>.
On Sat, 26 May 2001 14:19:25 -0500, you wrote:

>I believe this would break type saftey.  These are very carefully constructed to
>ensure that the proper hook fn is registered for the appropriate hook.
>
>I'll take a look at your patch later today and see what (if) it breaks anything,
>or if we were simply missing the "C" namespace wrappers.
>
The problem is that apr_array_push returns a void pointer.  And since
pHook is a predefined data type there is a type problem.  The extern
"C" wrappers only make the function behave using C linkage.  The code
within is still treated as C++.  I tried it and the same type cast
error still occured.

I think the only way to solve is this to use a type cast.  But correct
me if I am wrong.

Christian

>----- Original Message ----- 
>From: "Christian Gross" <Ch...@yahoo.de>
>Sent: Saturday, May 26, 2001 1:33 PM
>
>
>I was playing around with hooks and noticed that there is a problem
>with using hooks on C++.  The problem was that C++ does not allow a
>type conversion without a type cast.  Following is the fix
>
>---------------------------------------------------------------
>--- c:/httpd2_16/srclib/apr-util/include/apr_hooks.h Wed Apr  4
>01:35:46 2001
>+++ c:/httpd/srclib/apr-util/include/apr_hooks.h Sat May 26
>14:21:58 2001
>@@ -98,7 +98,7 @@
>
>_hooks.link_##name=apr_array_make(apr_global_hook_pool,1,sizeof(ns##_LINK_##name##_t));
>\
>  apr_hook_sort_register(#name,&_hooks.link_##name); \
>  } \
>-    pHook=apr_array_push(_hooks.link_##name); \
>+    pHook=(ns##_LINK_##name##_t *)apr_array_push(_hooks.link_##name);
>\
>     pHook->pFunc=pf; \
>     pHook->aszPredecessors=aszPre; \
>     pHook->aszSuccessors=aszSucc; \
>
>
>



Re: [PATCH] Type conversion needed for C++

Posted by Ben Laurie <be...@algroup.co.uk>.
"William A. Rowe, Jr." wrote:
> 
> I believe this would break type saftey.  These are very carefully constructed to
> ensure that the proper hook fn is registered for the appropriate hook.

In fact, it won't break type safety (there was none in the first place
in this statement, so it can't).

If it was an issue, we could always wrap it with a #ifdef cplusplus (or
whatever that manifest constant is). But in this case it isn't, so the
patch is fine by me.

Cheers,

Ben.

> I'll take a look at your patch later today and see what (if) it breaks anything,
> or if we were simply missing the "C" namespace wrappers.
> 
> ----- Original Message -----
> From: "Christian Gross" <Ch...@yahoo.de>
> Sent: Saturday, May 26, 2001 1:33 PM
> 
> I was playing around with hooks and noticed that there is a problem
> with using hooks on C++.  The problem was that C++ does not allow a
> type conversion without a type cast.  Following is the fix
> 
> ---------------------------------------------------------------
> --- c:/httpd2_16/srclib/apr-util/include/apr_hooks.h Wed Apr  4
> 01:35:46 2001
> +++ c:/httpd/srclib/apr-util/include/apr_hooks.h Sat May 26
> 14:21:58 2001
> @@ -98,7 +98,7 @@
> 
> _hooks.link_##name=apr_array_make(apr_global_hook_pool,1,sizeof(ns##_LINK_##name##_t));
> \
>   apr_hook_sort_register(#name,&_hooks.link_##name); \
>   } \
> -    pHook=apr_array_push(_hooks.link_##name); \
> +    pHook=(ns##_LINK_##name##_t *)apr_array_push(_hooks.link_##name);
> \
>      pHook->pFunc=pf; \
>      pHook->aszPredecessors=aszPre; \
>      pHook->aszSuccessors=aszSucc; \

--
http://www.apache-ssl.org/ben.html

"There is no limit to what a man can do or how far he can go if he
doesn't mind who gets the credit." - Robert Woodruff

Re: [PATCH] Type conversion needed for C++

Posted by "William A. Rowe, Jr." <ad...@rowe-clan.net>.
I believe this would break type saftey.  These are very carefully constructed to
ensure that the proper hook fn is registered for the appropriate hook.

I'll take a look at your patch later today and see what (if) it breaks anything,
or if we were simply missing the "C" namespace wrappers.

----- Original Message ----- 
From: "Christian Gross" <Ch...@yahoo.de>
Sent: Saturday, May 26, 2001 1:33 PM


I was playing around with hooks and noticed that there is a problem
with using hooks on C++.  The problem was that C++ does not allow a
type conversion without a type cast.  Following is the fix

---------------------------------------------------------------
--- c:/httpd2_16/srclib/apr-util/include/apr_hooks.h Wed Apr  4
01:35:46 2001
+++ c:/httpd/srclib/apr-util/include/apr_hooks.h Sat May 26
14:21:58 2001
@@ -98,7 +98,7 @@

_hooks.link_##name=apr_array_make(apr_global_hook_pool,1,sizeof(ns##_LINK_##name##_t));
\
  apr_hook_sort_register(#name,&_hooks.link_##name); \
  } \
-    pHook=apr_array_push(_hooks.link_##name); \
+    pHook=(ns##_LINK_##name##_t *)apr_array_push(_hooks.link_##name);
\
     pHook->pFunc=pf; \
     pHook->aszPredecessors=aszPre; \
     pHook->aszSuccessors=aszSucc; \