RAD Studio |  |

Explicit Conversion Operators (C++0x)
See Also
Collapse All

C++Builder 2009 includes support for explicit conversion operators, one of the features in the C++0x standard.

You can now apply the function specifier explicit in the definition of a user-defined conversion operator. Previously, explicit constructors (including copy constructors) were added to the language in order to prevent unintended conversions being implicitly called by the compiler. Now explicit conversion operators have been added to provide the same control over unintended conversion calls. .

Conversion functions declared as explicit work in the same contexts as explicit constructors (that is, direct-initialization, explicit type conversion). Explicit conversion operators produce compiler diagnostics in the same contexts (copy-initialization) as explicit constructors do.

For example:

Copy Code

    class T { };

    class X {

    public:

    explicit operator T() const;

    };

    void m() {

    X x;

    // with cast:

    T tc = (T)x;// ok

    // without cast:

    T t = x;// error: E2034

    // gets: Error E2034 x.cpp 13: Cannot convert 'X' to 'T' in function m()

    }

See Also

C++0x Features (C++Builder 2009)

Draft Working Paper

Summary of Working Group Papers

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!