Proxy objects...

B

barcaroller

What are proxy objects used for in C++? Currently, the only use of proxy
objects I know of is that one can write one-liner code such as:

myobj.foo() (a,b) (c,d) (e,f) ;

where myobj.foo() returns a proxy object with an overloaded operator()
member function, which returns a reference to the proxy object itself. But
writing one-liners cannot be reason enough to use proxy objects.
 
K

Kai-Uwe Bux

barcaroller said:
What are proxy objects used for in C++? Currently, the only use of proxy
objects I know of is that one can write one-liner code such as:

myobj.foo() (a,b) (c,d) (e,f) ;

where myobj.foo() returns a proxy object with an overloaded operator()
member function, which returns a reference to the proxy object itself.
But writing one-liners cannot be reason enough to use proxy objects.

The following come to mind:

a) Matrix classes which support [][] notation have an operator[] which
returns a proxy.

b) std::vector<bool> uses proxy objects instead of references (since those
won't do the trick).

c) String classes and the like might return proxy objects (e.g., for
operator[]) instead of references in order to avoid/solve aliasing
problems. This can be useful for COW and reference counted implementations.


Best

Kai-Uwe Bux
 
J

James Kanze

What are proxy objects used for in C++? Currently, the only
use of proxy objects I know of is that one can write one-liner
code such as:
myobj.foo() (a,b) (c,d) (e,f) ;
where myobj.foo() returns a proxy object with an overloaded
operator() member function, which returns a reference to the
proxy object itself. But writing one-liners cannot be reason
enough to use proxy objects.

The two most frequent reasons I know of are for context
dependent implicit type conversions (where the proxy has a:
template< typename T > Proxy::eek:perator T() const
function), and for distinguishing between lvalue and rvalue
accesses (where the proxy will have an implicit conversion to
the target type for rvalue accesses, and an operator= which
modifies the source, rather than the proxy, for lvalue
accesses).

I don't think I've ever seen a proxy used as you've described.
 
B

barcaroller

I don't think I've ever seen a proxy used as you've described.

boost::program_options

Example:

options_description desc("Allowed options");

desc.add_options()
("help", "help message")
("level", value<int>(), "set level");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top