Deleted functions

There is a more formal way to hide functions than using the scope. C++ will attempt to explicitly convert built-in types. For example:

    void f(int i);

You can call this with an int, or anything that can be converted to an int:

    f(1);     f('c');     f(1.0); // warning of conversion

In the second case, a char is an integer, so it is promoted to an int and the function is called. In the third case, the compiler will issue a warning that the conversion can cause a loss of data, but it is a warning and so the code will compile. If you want to prevent this implicit conversion you can delete the functions that you do not want callers to use. To do this, provide a prototype and use the syntax = delete:

    void f(double) = delete;  

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.