Dear all, I have been away from c++ for a while and I have just got back and started reading about "c++ concurrency in action".
I found an example where the author gives function witch returns thread instance but I can not understand how you can declare a function in a function definition:

std::thread f()
{
void some_function();
return std::thread(some_function);
}
std::thread g()
{
void some_other_function(int);
std::thread t(some_other_function,42);
return t;
}

It's not much different from declaring a function at file scope:

void some_function();

std::thread f()
{
    return std::thread(some_function);
}

The difference between this and your example is the visibility of some_function in the file. When the declaration is at file scope, everything after the declaration in the file can see it. When the declaration is at function scope, it's only visible within that function.

Deceptikon gave an amazing answer but I want to share what I figured out by accident yesterday.
You can declare a function inside a function via a data structure:

std::thread f()
{
    typedef struct
    {
        void somefunction(){}
    } t;

    return t.somefunction();
}

//OR

std::thread f()
{
    struct t
    {
        void somefunction(){}
    }
    t meh;

    return meh.somefunction();
}

the structs and the functions within them are limited to the outer function's scope. That means you can't access the struct or embedded functions from outside thread f();

Edited 4 Years Ago by triumphost

You can declare a function inside a function via a data structure

True. And because the OP is referencing material based around C++11, it couldn't hurt to mention that lambdas can also be used to create a nested function effect.

Edited 4 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.