Local function emulation (using struct) and scope of "static"

A

A

Here is an example function:

void MyFunction()
{
struct TLocal
{
static bool Check(int i)
{
return true;
}
};

TLocal::Check(1);
}

This is how I call local function "Check". This function should have scope
only within MyFunction as far as I know and lose scope once MyFunction is
done. Is this true?

If I remove "static" then TLocal::Check(1); call will not work - can anyone
explain why?

Also, is there any other (better in some way or easier in some way) method
to call local functions? Would it make some difference if I used "class"
instead of "struct"?
 
R

red floyd

Here is an example function:

void MyFunction()
{
struct TLocal
{
static bool Check(int i)
{
return true;
}
};

TLocal::Check(1);
}

This is how I call local function "Check". This function should have scope
only within MyFunction as far as I know and lose scope once MyFunction is
done. Is this true?

If I remove "static" then TLocal::Check(1); call will not work - can anyone
explain why?

Because then Check would need an object:

TLocal t;
t.Check(1);
Also, is there any other (better in some way or easier in some way) method
to call local functions?

There's really no other way to define "local" functions -- C++ does not
have nested/local functions.
Would it make some difference if I used "class"
instead of "struct"?

No. Classes and structs are identical, except for default access
specifiers.
 
G

Gert-Jan de Vos

Here is an example function:

void MyFunction()
{
struct TLocal
        {
        static bool Check(int i)
                {
                return true;
                }
        };

TLocal::Check(1);

}

This is how I call local function "Check". This function should have scope
only within MyFunction as far as I know and lose scope once MyFunction is
done. Is this true?
Correct.

If I remove "static" then TLocal::Check(1); call will not work - can anyone
explain why?

static allows you to call the Check function without first creating an
instance
(object) of the TLocal class.
Also, is there any other (better in some way or easier in some way) method
to call local functions? Would it make some difference if I used "class"
instead of "struct"?

Use the functor idiom:

void MyFunction()
{
struct Checker
{
bool operator()(int i)
{
return true;
}
} check;

check(1);
}

More or less the same but no static, no TLocal::. The syntax of a
local functor is further reduced by C++0x's lambda's.
 
T

thomas

Here is an example function:

void MyFunction()
{
struct TLocal
        {
        static bool Check(int i)
                {
                return true;
                }
        };

TLocal::Check(1);

}

This is how I call local function "Check". This function should have scope
only within MyFunction as far as I know and lose scope once MyFunction is
done. Is this true? Yeap..


If I remove "static" then TLocal::Check(1); call will not work - can anyone
explain why?
Without static you need an object to invoke a method.
Also, is there any other (better in some way or easier in some way) method
to call local functions? Would it make some difference if I used "class"
instead of "struct"?

Why do you want "local functions"(your words)?
Maybe the only reanable explanation is avoiding polluting name space.
Then declare a class wrapping your "MyFunction()" interface and make
your local function private.
Or define a new namespace.
Either way is better than your "local function".
 
A

A

Why do you want "local functions"(your words)?
Maybe the only reanable explanation is avoiding polluting name space.
Then declare a class wrapping your "MyFunction()" interface and make
your local function private.
Or define a new namespace.
Either way is better than your "local function".

i need them because in the same function i have a set of actions that are
always identical with one parameter as exception. so i can fit them nicely
in a local function within a function that has only this parameter
different. otherwise i would have to repeat a block of code over and over
which would make a mess.

as i have no use of this block of code anywhere else than in this particular
function it makes sense to use such "local functions".

unless you have a better idea how to place a repeatable block of code that
is used only in that function?
 
W

werasm

Why do you want "local functions"(your words)?

Because sometimes the code in the local function is only applicable
(usable) from one function (and makes no sense from other contexts).
I've found that sometimes this prevents (local) code duplication.

Should I then now go and stick it in some unnamed namespace, think out
some name for it and confuse others as to its use, or would "local" be
more encapsulating?
Maybe the only reanable explanation is avoiding polluting name space.

This would not be the case if we use an unnamed namespace, but our
name would have to be unique, not?
Then declare a class wrapping your "MyFunction()" interface and make
your local function private.

I hope you don't suggest that I should pollute my interface, which for
me is worse than polluting some namespace. Anything that need not be
in my interface should not be there. Perhaps a pimpl with access to
the this pointer would suffice as local (but still not local enough).
Or define a new namespace.
Either way is better than your "local function".

I for one, don't agree with this in all circumstances. I like Gert-
Jan's idiom and I've often used it.

Regards,

Werner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top