# C++ Practices from Chromium Code Style: Avoiding Copies and Using Debug Checks

DevFeed: [C++ Practices from Chromium Code Style: Avoiding Copies and Using Debug Checks](<https://devfeed.tech/articles/potentially-neat-c-protipz-35539.md>)

Original publisher: [Read original article](<https://meowni.ca/posts/protipz/>)

Author: Monica Dinculescu

Published: 2014-01-20T00:00:00Z

Content type: tutorial

Language: en

Sources: [Monica Dinculescu](<https://devfeed.tech/sources/monica-dinculescu.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Chromium](<https://devfeed.tech/topics/chromium.md>), [code style](<https://devfeed.tech/topics/code-style.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [debug](<https://devfeed.tech/topics/debug.md>), [test-coverage](<https://devfeed.tech/topics/test-coverage.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [chromium](<https://devfeed.tech/tags/chromium.md>), [clang](<https://devfeed.tech/tags/clang.md>), [code-style](<https://devfeed.tech/tags/code-style.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [debug](<https://devfeed.tech/tags/debug.md>), [test-coverage](<https://devfeed.tech/tags/test-coverage.md>)

## AI overview

A practical C++ article based on Chromium code style. It discusses preventing unwanted copy construction and assignment with a macro, and using debug-only checks to catch bad scenarios during development and testing.

## Source excerpt

Disclaimer: these aren't new protipz. I didn't make them up. They're actually straight out of the Chromium code style, they're pretty trivial, and you might already use them. But just in case you're not a Chromium committer (the outrage), or are fairly new at C++ and want to make your code less suck, here they are. I think they're neat. Copy constructors and their brethren You know that scene from The Fly when Jeff Goldblum, having not screwed up teleporting a small baboon, decides he should totally teleport himself? But then he screws that up (because software), manages to turn himself into a giant terrifying fly (because David Cronenberg), and continues to give me nightmares as an adult. That's exactly how I feel about copy constructors. You can absolutely get them right, but they're a pain, and among other crimes they're committing, they're sometimes deceivingly slow. The point is, most of the time you don't even need them. I mean, Jeff Goldblum teleported himself like three meters away. Couldn't he have just walked? What we tend to do instead is convince the compiler to get annoyed with us if we try to use a copy constructor. This is easy because the compiler <3s being annoyed with us. So we can define a nice macro (stay with me) that adds a private declaration, but doesn't implement it: #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ void operator=(const TypeName&) Which you would then use in your private section of your class, like so: class Hooman { public: Hooman(); ~Hooman(); private: DISALLOW_COPY_AND_ASSIGN(Hooman); }; Now, when you try to be ambitious and clone Jeff Goldblum, Hooman jeffGoldblum; Hooman teleportedJeffGoldblum(jeffGoldblum); Clang will tell you something like "error: calling a private constructor of class 'Hooman'". Other compilers might tell you other things, but they'll generally have the same annoyed tone. Now would be a good time to apologize to your compiler for all the silly things you've done in the past.