James Harris <ja************@googlemail.comwrites:
On 19 Aug, 11:31, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>In article
<07da609e-11bf-4901-8741-70a54d147...@n33g2000pri.googlegroups.com>,
holysmoke <sukrit.me...@gmail.comwrote:
>I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,
You can't. Anything the macro expands to, the user could just
write themselves.
Is there a way to use the preprocessor to generate a dynamic name for
the function - based on time of day or something random, even the line
number, as long as it's not known in the calling code - and then call
that from within the macro? Assemblers sometimes permit this. I don't
know about the C preprocessor.
I can't think of a way to do this from the C preprocessor, but you
could always pre-filter the source code. Something like this:
void func_$(unsigned int t, unsigned int lineno)
{ ... }
#define func(t) (func_$((t), __LINE__))
where your own pre-filter converts each occurrence of $ to some random
number (the same one for each occurrence in the same file).
But really, what does this buy you? For the macro to be useful, you
need to provide the file that defines it to anyone who's going to use
it. Your users can always just take a look at it and see that
func(42)
expands to
(func_845285045997((42), __LINE))
Anything that you can do in a macro, your users will be able to do
themselves.
Assume for the sake of argument that a solution exists, something that
absolutely prevents users from calling your function other than via
the macro. What real advantage would this have over just (a) making
it convenient for users to use the macro, and (b) telling them they
shouldn't call the function directly? Something like this:
void do_not_call_directly(unsigned int t, unsigned int lineno)
{ ... }
#define func(t) (do_not_call_directly((t), __LINE__))
If you really need to *prevent* hostile users from calling your
function directly, that's a much harder problem, and one that I
suspect can't be solved within standard C. If you can just tell them
"If you call this function directly, all bets are off; I reserve the
right to change the name at the next full moon", that's a lot easier.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"