Class Template const_view

Description
Note
Headers
Reference
Synopsis
Class Template const_view
Example

Description

The class template const_view is an interface adapter which takes an interface as a template parameter and yeilds a const view of the interface: an interface with the same properties as the original interface except that it can be bound to const objects and its non-const member functions have been disabled. Attempting to invoke a non-const member function of an instance of a specialization of const_view produces a compile-time error.

Note

In the future it will be possible to use const_view in conjunction with fixed_view to produce an interface whose non-const member functions have been disabled and which cannot be rebound. See Future Directions: Qualifiers.

Headers

<boost/interfaces/const_view.hpp>

Reference

Synopsis

namespace boost { namespace interfaces {

template<typename Interface>
class const_view {
public:

        // Constructors

    const_view();

    template<typename T>
    const_view(const T& t);

    template<typename Subinterface>
    const_view(const Subinterface& other);

    const_view(const const_view& other);

    template<typename Subinterface>
    const_view(const const_view<Subinterface>& other);

        // Assignment

    template<typename T>    
    const_view& operator=(const T& rhs);
    
    const_view& operator=(const const_view& rhs);
    
    const_view& operator=(unspecified-null-pointer-type);

    operator unspecified-bool-type() const;

    [const member functions from Interface]
};

} } // End namespace boost::interfaces

Class Template const_view

Template parameters
Interface-

An interface defined using the Interface Definition Language (IDL).

const_view::const_view

    const_view();

Constructs an unbound const_view. Never throws.

    template<typename T>
    const_view(const T& t)

Constructs a const_view bound to the given object. Never throws.

    template<typename Subinterface>
    const_view(const Subinterface& other);

Constructs a const_view bound to the same object as the given interface. Subinterface must be the same as or a subinterface of the template parameter Interface. Never throws.

    const_view(const const_view& other);

    template<typename Subinterface>
    const_view(const const_view<Subinterface>& other);

The first member constructs a copy of the given const_view. The second member constructs an instance of const_view bound to the same object as the given instance of const_view<Subinterface>. Subinterface must be a subinterface of the template parameter Interface. Neither member throws.

const_view::operator=

    template<typename TT>
    const_view& operator=(const T&);

Binds this instance of const_view to the given object. Never throws.

    const_view& operator=(const const_view& other);

Binds this instance of const_view to the same object as the given interface. Never throws.

    template<typename TT>
    const_view& operator=(unspecified-null-pointer-type);

Causes this instance of const_view to become unbound from the current object, if any. The argument must be a null pointer constant, e.g., 0. Never throws.

const_view::operator unspecified-bool-type()

    operator unspecified-bool-type() const;

Returns an unspecified value which, when used in boolean contexts, is equivalent to true if this instance of const_view is bound to an object and to false otherwise. Never throws.

Example

By declaring a function to take a const view of an interface, one can guarantee that non-const functions will not be accessible within the body of the function:

// Pseudocode:
interface IExplosive {
    std::string type() const;
    void detonate();
}

void f(const_view<IExplosive>& e)
{
    std::cout << e.type() << "\n";
    e.detonate(); // Compile-time error.
}

Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah