Hi guys ,
How to run some part of program before int main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
void startUp()
{
cout << "I am running before main" << endl;
}
void clean()
{
cout << "I am running after main" << endl;
}
int main()
{
cout << "I am inside main" << endl;
return 0;
}
I want ouput as :
I am running before main
I am inside main
I am inside main
#include <iostream>
#include <cstdlib>
// All non-local variables with static storage duration are initialized as part of program startup,
// before the execution of the main function begins (unless deferred)
// http://en.cppreference.com/w/cpp/language/initialization#Non-local_variablesstaticauto& cout_alias = std::cout << "I am running before main\n" ;
void run_this_at_the_end() { std::cout << "I am running after main\n" ; }
struct A
{
A() { std::cout << "this is A::constructor\n" ; }
~A() { std::cout << "this is A::destructor\n" ; }
};
staticconst A a ; // destroyed after the last statement in main is executed
int main()
{
// It is implementation-defined whether dynamic initialization happens-before the first statement
// of the main function (for statics) ... or deferred to happen after.
// If the initialization is deferred to happen after the first statement of main/thread function,
// it happens before the first odr-use of any variable with static/thread storage duration
// defined in the same translation unit as the variable to be initialized.
// http://en.cppreference.com/w/cpp/language/initialization#Non-local_variablesconst A* unused = std::addressof(a) ; // odr-use of a
cout_alias << "-------------\nentered main()\n" ; // odr-use of cout_alias
// http://en.cppreference.com/w/cpp/utility/program/atexit
std::atexit( run_this_at_the_end ) ;
std::cout << "about to exit main()\n-----------------\n" ;
}
Hi JLBorges
Thank you very much But I didn't understood static auto& cout_alias . i.e is cout_alias is inbuilt variable ? Please never mind if it is silly question I am still learning C++
1 : Why did you used static const ?
Also can you please explain below code segment
> But I didn't understood static auto& cout_alias . i.e is cout_alias is inbuilt variable ?
No, it is just a variable name like any other name. We could have instead written:
1 2
// static auto& cout_alias = std::cout << "I am running before main\n" ;
static std::ostream& a_name = std::cout << "I am running before main\n" ;
and then, in main: a_name << "-------------\nentered main()\n" ; // odr-use of a_name
> Why did you used static const ?
In staticconst A a ;
the const-qualifier specifies that the object is not modifiable.
In this context, static specifies internal linkage ie. the name a is not programatically visible in other translation units. (It is superfluous here; the default linkage of a const-qualified, but not volatile-qualified variable is internal.)
A a ; would have sufficed for the point I was attempting to make. static const came out of force of habit; instinctively, I limit the visibility of names and the mutability of objects.
Non-standard GNU extensions which are often useful in C, but completely unnecessary in C++
The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes