Anonymous object outside main

I have such helper class/struct to call functions on initialization, and I need to create anonymous variables for it, but C++ does not allow it outside main(), why so?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct execute{
  execute(std::function<void()> f){
    f();
  }
}

execute([]{
  std::cout<<"not working\n";
});
int main(){
  execute([]{
    std::cout<<"works!\n";
  });
  return 0;
}

Why in the first case the compiler gives me errors and in the second case it works?
Outside of functions you can only declare variables. If you want execute([]{std::cout<<"not working\n";}); to run at the start of the program why not place it at the beginning of main()?
@Peter87 As you can see, execute is a struct an I'm declaring an instance of it, if I give it a name then it works, but I want it to be anonymous, so it will be destroyed instantly. Can this be done? Also I can't place it on top of main because I'm making a library and I won't be in charge of main.
closed account (48bpfSEw)
just a hint: initialization, finalization

http://stackoverflow.com/questions/4403002/why-is-there-no-initialization-keyword-in-c-as-there-is-in-delphi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <functional>

namespace my_library {

    namespace detail {

        struct executor { explicit executor( std::function<void()> f ) { f() ; } };
        bool do_execute( std::function<void()> f ) { f() ; return true ; }

        const bool init_done = ( executor( []{ std::cout<<"working\n"; } ), 
                                 do_execute( []{ std::cout<<"also working\n"; } ) ) ;
    }
}

http://coliru.stacked-crooked.com/a/911574e02c808f00
You may want to read this: http://www.petebecker.com/js/js199905.html
@JLBorges, sorry your solution does not fit my needs.
So there's no way to declare an anonymous object/variable/instance outside main?
An anonymous object/variable/instance declaration needs to be within a function. The reason C++ won't allow you to have it is that it simply doesn't make sense.

A temporary variable that gets created is more often than not part of a larger expression (and as we know, expressions can only be contained inside functions or member functions). After the particular statement that uses the expression is over, the temporary instance goes out of scope as well and can no longer be used.

Since expressions cannot be placed outside of functions, it simply doesn't make sense to allow the declaration of temporary variables (which are used with expressions) outside of functions.

Lastly, one very important confusion could occur if C++ DID allow this:
- As you know, declaring a temporary instance can occur like this (Assume I have class Game):
 
Game();


- Outside of a function, the compiler will think we are declaring either a new function, or a constructor for a class type (or some other hodgepodge) and it will just get confused and say typename not recognized.

In short, temporary instances and variables can only go into function bodies.

Hope this helped,
Joe

Last edited on
Topic archived. No new replies allowed.