I think it's entirely circumstantial. Only as a very last resort.
A toddler might need a tiny smack on the hand if they're trying to do something dangerous for example. But distraction or other methods might work better, depending on the specific situation.
I think the key is to be consistent and try not to do things for convenience. By that I mean, if you tell a toddler whining loudly that they're not going to get a chocolate bar for their behavior, but then immediately hand said chocolate bar to the kid once you get too fed up with the noise, what is that teaching the child? That throwing a tantrum will get them what they want.
I have seen this many times at my work, mostly from very embarrassed parents just wanting the kid to shut up. Sometimes it's just clearly overworked parents or parents too lazy to return an item their kid/s have sneaked into the trolley or even just the poor stereotypical "soft touch" father trying to be stern but immediately caving into the demands of their little girl. Whilst I can certainly understand and sympathize (I mean how many people want to be the "bad cop" to their little ones or be seen not doing anything to quell a temper tantrum in public?) I also think that being a little embarrassed is worth imparting discipline when needed. Cave when they're being good, distract them if you can or just dash out to the car as fast as possible. Which also happens quite a lot at my work lol!
When you have rules, stick to them, be consistent and kids will usually respond. Some kids might need a bit of extra discipline and some kids might be the type that you should never hit in your life (like for instance I would never in a million years hit an autistic child. That's just barbaric.)
I would never endorse corporal punishment in a school though. Parents, ehhh, their choice as long as it's not beatings or other types of abuse. But teachers? No. Too much risk of an overworked adult taking out their frustrations on the kids.
Having said that, however, I would not be inclined to condemn a teacher who hits a teen in self defense. Far too often kids are hitting teachers because they think they're untouchable and they kind of are with the lax laws. Public schools, man. If you ain't a tough teacher, you is ****ed. I know this because in grade 10 we kind of chased away our English teacher. Though in my defense, he clearly did not have what it took to be a teacher. No authoritative air about him.