Why do c++ related topics get downvoted so badly?
Because the C++ community cares about having quality content, answering quality questions, and helping people that have problems with their questions fix those problems, rather than providing low quality answers to low quality questions.
Other topics get equally silly, non well stated, non-clear, not formatted questions, but honestly I have not seen this hostility elsewhere.
It's not hostility, it's them being helpful, having standards and values, and enforcing them. This is how it's able to produce quality content.
It's also worth noting that some tag communities do actually have differing amounts of low quality content. They all invariably have some, but there are those with more than others. When you see tags with a higher or lower average score it's a combination of the quality of the questions in those tags, and also the standards that those communities hold themselves to, not just one or the other.
It is obvious that these practices only harm the community
Oh, and how so? Care to provide evidence to back up this assertion?
and they actively discourage people (specially beginners) to participate.
It discourages people that consistently provide low quality content, yes. When given feedback that their contributions are problematic they can either choose to fix the content, and improve the quality of their contributions, or they may choose not to. Either choice is preferable than them continuing to provide low quality content.
Is there a mechanism to prevent this type of toxic behavior?
It's not toxic. It's helpful. And we don't want to prevent it. We want to encourage people to downvote bad posts, to close questions that merit closure. These are precisely the mechanisms that have helped the site create the quality content that it has, and why so many other sites largely fail to provide useful content.