Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

self referring C++ function definition

46 views
Skip to first unread message

Lynn McGuire

unread,
Jul 31, 2018, 10:42:05 PM7/31/18
to
Can I make a C++ function definition that refers to itself ?

For example, it would be something like this:

void myfunction (int * nin, int * nocomp, double temperatures [ * nin],
double components [ * nocomp] [ * nin])
{

}

BTW, this is a Fortran 77 called function so everything is a pointer.

Thanks,
Lynn

Ben Bacarisse

unread,
Jul 31, 2018, 11:03:23 PM7/31/18
to
Lynn McGuire <lynnmc...@gmail.com> writes:

> Can I make a C++ function definition that refers to itself ?

I was all set to say "yes", but then your example shows no reference to
the function being defined. In general, C++ functions can call
themselves and take a pointer to themselves.

> For example, it would be something like this:
>
> void myfunction (int * nin, int * nocomp, double temperatures [ * nin],
> double components [ * nocomp] [ * nin])
> {
>
> }

This is a C function using variably modified types. C++ does not have
such types. You might want to post in comp.lang.c instead.

> BTW, this is a Fortran 77 called function so everything is a pointer.

If the Fortran function calls itself (if that is what you mean) you may
need to ask in a Fortran group. Fortran 77 did not say that
implementations must support recursion, though many did. I think it was
Fortran 90 that first required it.

--
Ben.

Rick C. Hodgin

unread,
Jul 31, 2018, 11:23:25 PM7/31/18
to
On 7/31/2018 10:41 PM, Lynn McGuire wrote:
> Can I make a C++ function definition that refers to itself ?
>
> void myfunction(int* nin, int* nocomp, double temperatures[*nin],
>                double components [*nocomp][*nin])
> {
> }
>
> BTW, this is a Fortran 77 called function so everything is a pointer.

To my knowledge, C++ doesn't provide variable limits like this which
are enforceable beyond constant definitions at compile-time. You have
to define the base operation of the passed parameters as an incoming
stream of double elements, and then resolve the offset to each element
in the array locally within their scope as a calculation of sequential
elements:

void myfunction(int* nin, int* nocomp, double temps[], double comps[])
{
// Reference elements within the array data sequentially
#define el(a, b) ((a * *nin) + b)

// Access temps sequentially
for (int n = 0; n < *nin; ++n) // n = *nin
cout << temps[n] << endl;

// Access comps sequentially
for (int c = 0; c < *nocomp; ++c) // c = *nocomp
{
for (int n = 0; n < *nin; ++n)
cout << comps[el(c, n)<< endl;
}
}

--
Rick C. Hodgin

Rick C. Hodgin

unread,
Jul 31, 2018, 11:26:37 PM7/31/18
to
> ===>       cout << comps[el(c, n)<< endl;
>     }
> }


Not sure what happened, but the line above should've been:

cout << comps[el(c, n)] << endl;

I've noticed many of my posts having odd errors like this after I've
posted them lately. It's happened often enough on a wide enough range
of target media platforms that I've begun to wonder if it's me missing
the mistakes I've made, or if there's something else happening.

--
Rick C. Hodgin

James Kuyper

unread,
Aug 1, 2018, 12:01:49 AM8/1/18
to
On 07/31/2018 10:41 PM, Lynn McGuire wrote:
> Can I make a C++ function definition that refers to itself ?
>
> For example, it would be something like this:
>
> void myfunction (int * nin, int * nocomp, double temperatures [ * nin],
> double components [ * nocomp] [ * nin])

In C++, the dimensions of arrays are required to be constant expressions
(8.3.4p1), so this isn't permitted.

This is a difference from C, where your code would be perfectly
acceptable. It would declare "components" as a Variable Length Array
(VLA). "temperatures" looks like a VLA, but any declaration of a
function parameter that looks like an array is implicitly converted into
a pointer declaration, with the length specified for that array being
ignored (unless preceded by the 'static' keyword). Thus, in C, that
declaration would be exactly equivalent to

void myfunction (int * nin, int * nocomp, double * temperatures,
double (*components) [ * nin])

> {
>
> }
>
> BTW, this is a Fortran 77 called function so everything is a pointer.

I'm curious - what part of that code gives you the impression that the
function refers to itself?
Later parameters in your parameter list refer to the value of earlier
parameters in that list - is that what you're talking about? In itself,
that's perfectly permissible because each parameter has a scope that
begins as soon as it's declarator is complete. (C6.2.1p7, C++ 3.3.3p2).
However, this particular way of using previously declared parameters in
the declarations of later parameter is permitted only in C, and not in C++.

Lynn McGuire

unread,
Aug 1, 2018, 1:20:30 PM8/1/18
to
Thanks !

And it is the aging process. I am 58 and the C++ compiler saves my
posterior again and again with simple coding mistakes. The F77
compiler, rarely happens.

Lynn


Lynn McGuire

unread,
Aug 1, 2018, 1:21:55 PM8/1/18
to
I meant that the latter portion of the arguments refer to the former
portion of the arguments.

Thanks,
Lynn


Lynn McGuire

unread,
Aug 1, 2018, 1:22:15 PM8/1/18
to
Yes, I meant that the latter portion of the arguments refer to the

Rick C. Hodgin

unread,
Aug 1, 2018, 1:30:27 PM8/1/18
to
On 8/1/2018 1:20 PM, Lynn McGuire wrote:
> Thanks !

You're welcome.

> And it is the aging process.  I am 58 and the C++ compiler saves my posterior
> again and again with simple coding mistakes.  The F77 compiler, rarely happens.


I'll be 49 soon, and I find my ability to code is increasing in many
ways, and decreasing in many others. I can't work as hard or as long
as I used to, but I'm more experienced now and can do in less steps
what I would've taken more steps on previously. On the whole, I've
lost something with aging, but am still effective. And every now and
again I find myself back "in the zone" for brief spells ... Feels good
when it happens. :-)

Reminds me of an old country song I used to hear when I worked at
my dad's welding shop:

"I ain't as good as I once was. I got a few years on me now.
But there was a time, back in my prime, when I could really
hold my own..."

:-)

--
Rick C. Hodgin

Ben Bacarisse

unread,
Aug 1, 2018, 3:20:15 PM8/1/18
to
Lynn McGuire <lynnmc...@gmail.com> writes:

> On 7/31/2018 10:03 PM, Ben Bacarisse wrote:
>> Lynn McGuire <lynnmc...@gmail.com> writes:
>>
>>> Can I make a C++ function definition that refers to itself ?
>>
>> I was all set to say "yes", but then your example shows no reference to
>> the function being defined. In general, C++ functions can call
>> themselves and take a pointer to themselves.
>>
>>> For example, it would be something like this:
>>>
>>> void myfunction (int * nin, int * nocomp, double temperatures [ * nin],
>>> double components [ * nocomp] [ * nin])
>>> {
>>>
>>> }
<snip>
> I meant that the latter portion of the arguments refer to the former
> portion of the arguments.

Oh I see. Not in that way, no.

I think C++ missed a trick here. While C++ has better alternatives to
C's variable length arrays (and retrofitting them to C++, even in 1999,
would have been a hassle), it could have adopted the corresponding
variably modified types. In effect, these just give the compiler the
stride of an array so that it can do 2D (or higher) indexing.

Like all such throw-away language design suggestions, I may well be
missing all sort of complexities, but being able to index into 2D arrays
without lying about the types seem worth a shot.

Of course in pure C++ you'd probably do it some other way, but when
interfacing with external formats, it could be helpful.

--
Ben.

James Kuyper

unread,
Aug 1, 2018, 7:45:02 PM8/1/18
to
On 08/01/2018 11:00 AM, Stefan Ram wrote:
> James Kuyper <james...@alumni.caltech.edu> writes:
>> I'm curious - what part of that code gives you the impression that the
>> function refers to itself?
>
> IIRC that thing which is /not/ possible is to write a
> function whose type correctly reflects that the function
> returns a pointer to itself.

He's made it clear that what he was actually asking about was whether
the declaration of a function parameter can refer to the value of
previously declared parameters. It has nothing to do with the issue
you're referring to.
The answer to his actual question is "No" in C++, though it is permitted
in C.

Note: your description of the other problem is a bit more specific than
it should be. The return value of the function doesn't have to be a
pointer to itself; it could be a pointer to any other function of the
same type, and the problem would still come up.
When using function prototypes, the problem also comes up if any
argument of the function is a pointer to a function of the same type.

Lynn McGuire

unread,
Aug 2, 2018, 6:18:27 PM8/2/18
to
I ended up just building the function as follows:

extern "C" void __pragma ("my_prag") spreadsheet
( int & nin, int & nout, int & nocomp, int & neqp, int & ndsp,
double sivpfr [], double sitemp [], double sienth [], double
sientr [],
double simole [], double sicomp [], double sikv [],
double sovpfr [], double sotemp [], double soenth [], double
soentr [],
double somole [], double socomp [], double sokv [],
type64 eqpar [], type64 despar [])

Thanks,
Lynn
0 new messages