Tuesday, January 22, 2008

c++: function nested inside other functions

Everybody knows that you can't define function inside other function. I want to argue:

int
main(int argc, char **argv)
{
    struct Nested
    {
        static void print()
        {
            std::cout << __func__ << std::endl;
        }
    };
    Nested::print();
    
    return 0;
}
Or you can apply some macros magic:
#define NESTED_FUNCTION_BEGIN(UNIQUE_NAME) \
    struct UNIQUE_NAME { static
#define NESTED_FUNCTION_END };

int
main(int argc, char **argv)
{
    NESTED_FUNCTION_BEGIN(Nested)
    void print()
    {
        std::cout << __func__ << std::endl;
    }
    NESTED_FUNCTION_END

    Nested::print();

    return 0;
}

No comments: