Programming in C++ without if/else/for/while/etc..

Recently on of my friends challenges me to write a game in C++ without using any branching and looping expressions. It’s really not difficult at all. I could write a game in C and compile it with the M/o/Vfuscator then disassemble it, put all the assembly instructions into my C++ code. Bobs your uncle, challenge done.

But what’s the fun doing that.

Challenge-Accepted-Meme

Let’s start

I’m planning on implementing the forbidden keywords in a way that it looks like the original keyword.

The following code shown will need a compiler that supports C++14 to compile. I’m using GCC 6.3.1 and clang 3.9.1(in fact, both supports C++17). If you are using any other compiler use Google to find out the required compiling flag or even if your compiler supports C++14.

If

Let’s start with implementing if. The closest syntax using a function to mimic if would be passing a bool value and 2 lambdas. One for the first branch and the other for the other branch.

If(me.isHandsome(),[]()
{
	cout << "True story." << endl;
},[]()
{
	cout << "No, I am handsome." << endl;
})

 

The only way I could came up to implement it is by using a jump table. But you can’t make an array of lambdas in C++ since every lambda have a different type event they have the same signature(returns the same type and takes in the same types of parameters).

The solution is std::function . It can take in any callable and turn it into a function.

So, the implementation ends up looking like this.

template<typename TFunc, typename FFunc>void If(bool condition, TFunc tFunc,
FFunc fFunc)
{
	std::function<void()> jumpTable[2] = {fFunc,tFunc};
	jumpTable[static_cast<int>(condition)]();
};

template<typename TFunc>void If(bool condition, TFunc tFunc)
{
	If(condition, tFunc, [](){});
};

While

We need a loop to do mostly every thing. And the while loop is the simplest one. Let’s implement it first. The implementation is easy by using If from above and recursion.

template<typename ConditionFunc, typename Func>
void While(ConditionFunc conditionFunc, Func func)
{
	auto compFunc = [&]() -> bool {return conditionFunc();};
	If(compFunc(),
	[&](){
		func();
		While(conditionFunc, func);
	});
}

This gives up the ability to do a basic loop. Such as

int i;
While([&](){return i<10;},[&]()
{
	cout << i << endl;
	i++;
});

For

Looking a the for loop’s documentation. This is the loop we use a lot in C++. Here is the implementation I came up with

template<typename InitFunc, typename ConditionFunc
, typename ItFunc, typename Func>
void For(InitFunc initFunc, ConditionFunc conditionFunc
, ItFunc itFunc, Func func)
{
	initFunc();
	While(conditionFunc, [&]()
	{
		func();
	itFunc();
	});
}

Yeah…. Using 4 lambdas a parameters for a function is bad. It look nasty… This is how the for loop looks like. I;m seeing that I’ll ended up using the while loop a bit more in the game.

int i;
For([&](){i=0;},[&](){return i<10;},[&](){i++;},[&]()
{
	cout << i << endl;
});

Here is the implementation of all the functions above and demo.

The Game

I ended up don’t have time to write the game out of that. I guess my friend wins. I might write it in the future.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: