In the U.S. cockpit visits are banned, but not when you are at the gate. I flew a DL722 from SAN-SLC last summer, and had the opportunity to visit the flight deck while we were still parked at the gate and pax were boarding, also had a chance to chat with the pilots about how they got their jobs, my goal of becoming a pilot, ops of the 727 etc. :d
It was a great visit that I will remember for a long time, last all of 5-7 minutes but it was worth it.
Should be allowed in certain instances. There is the risk of hijacking and/or pax playing with controls, however, this is only really dangerous when the plane is actually in the air, taxiing, or on T/O/Landing roll.
Therefore I think the compromise should be that pax are only allowed to visit the flight deck briefly while the aircraft is at the gate, either after or before the flight, preferably after.
That way, there is alot less of a safety concern!!
Now if they banned visits on the ground it would be terrible. I thoroughly enjoyed speaking to the pilots before and after the flight and would love to still have the opportunity to do that again!