We have a definition of when someone is in melee.
From the Combat chapter of the PHB:
Two characters are engaged in melee if they are enemies of each other and either threatens the other. (An unconscious or otherwise immobilized character is not considered engaged unless he is actually being attacked.)
I have a longsword. The orc has a greataxe. We're 5 feet apart. We're engaged in melee - I threaten the orc, and the orc threatens me. If I attack the orc with my longsword, I'm taking the attack action in melee combat, so I can use Combat Expertise.
I have a longbow. The orc has a greataxe. We're 5 feet apart. We're engaged in melee - I do not threaten the orc, but the orc threatens me. If I attack the orc with my longbow (provoking an AoO), I'm taking the attack action in melee combat, so I can use Combat Expertise.
I have a longbow. The orc has a greataxe. We're 10 feet apart. We're not engaged in melee - I do not threaten the orc, and the orc does not threaten me. If I attack the orc with my longbow, I'm taking the attack action but I'm not in melee combat, so I cannot use Combat Expertise.
I have a longbow. The orc has a longspear. We're 10 feet apart. We're engaged in melee - I do not threaten the orc, but the orc threatens me. If I attack the orc with my longbow (provoking an AoO), I'm taking the attack action in melee combat, so I can use Combat Expertise.
I am unarmed. The orc is unarmed. We're five feet apart. We're not engaged in melee - I do not threaten the orc, and the orc does not threaten me. If I attack the orc with my unarmed strike, I'm taking the attack action but I'm not in melee combat, so I cannot use Combat Expertise.
-Hyp.