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

Discussion in 'C++' started by A, May 20, 2010.

  1. A

    A Guest

    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"?
    A, May 20, 2010
    #1
    1. Advertising

  2. A

    red floyd Guest

    On 5/19/2010 8:35 PM, A wrote:
    > 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.
    red floyd, May 20, 2010
    #2
    1. Advertising

  3. On May 20, 5:35 am, "A" <> wrote:
    > 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.
    Gert-Jan de Vos, May 20, 2010
    #3
  4. A

    thomas Guest

    On May 20, 11:35 am, "A" <> wrote:
    > 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".
    thomas, May 20, 2010
    #4
  5. A

    A Guest

    > 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?
    A, May 20, 2010
    #5
  6. A

    werasm Guest

    On May 20, 1:38 pm, thomas <> wrote:

    > 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
    werasm, May 21, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    944
    Paul Opal
    Oct 11, 2004
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,830
    Christian Janßen
    Aug 8, 2003
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,372
    Chris Fogelklou
    Apr 20, 2004
  4. Ole
    Replies:
    4
    Views:
    596
    Michael Wojcik
    Oct 26, 2004
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,363
    James Kanze
    Dec 19, 2008
Loading...

Share This Page