PrevUpHomeNext

esvcpp::main


Big hands and small hands work together to save wild animals.

esvcpp::main main entry point

esvcpp c++ esvcpp::main main entry point.

esvcpp::main Hello World

Return 0 by default,

#include <esvcpp/main.hpp>

void esvcpp::main()
{
	std::cout << "Hello ";
	esv::print("World");
}	// return 0 by default

include esvcpp/main.cpp implies include these headers:

self.set_return(value)

The return value of main is set by self.set_return(value), not by return statement.

#include <esvcpp/main.hpp>

void esvcpp::main()
{
	std::cout << "set return value to 123" <<std::endl;
	self.set_return(123);
}	// return value is set to 123

catch exception by default

The main exception will be caught by default, this can make some levels of lazy.

#include <esvcpp/main.hpp>

void esvcpp::main()
{
	std::error_code ec;
	throw std::system_error{ec, "Test c++ exception"};
}	// exception will be caught and handled even if you do not write try...catch... block.

output:

[c++ main exception] [std::exception] in main:
        Test c++ exception: Success

self.enable_tcrash(true/false)

#include <esvcpp/main.hpp>

void esvcpp::main()
{
	self.enable_tcrash(true);
	throw std::runtime_error{"c++ standard exception"};
}

After self.enable_tcrash(true); (it is false by default), throw exception will be played as "pseudo crash" .

output:

terminate called after throwing an instance of 'std::runtime_error'
  what():  [c++ main exception] [std::exception] in main:
       c++ standard exception
Aborted

argc argv

argc and argv are stored in args, that its type is std::vector<std::string>,

argc is the vector size, each argv is std::string type.

#include <esvcpp/main.hpp>

void esvcpp::main()
{
	std::cout << "argc-> " << args.size() << std::endl;
	
	std::cout << "argv:\n";
	for (const std::string_view arg: args)
		std::cout << "\t" << arg << std::endl;
}

./main5 ls pwd whoami

output:

argc-> 4
argv:
	./main5
	ls
	pwd
	whoami

See Also

esv::print

esv::printnl


PrevUpHomeNext

E