Boost logo

Boost :

Subject: Re: [boost] [property] interest in C# like properties for C++?
From: Sid Sacek (ssacek_at_[hidden])
Date: 2009-10-19 19:02:05

Sorry, but your design is flawed.

This code should compile according to your design and the C# rules, but it doesn't. For it to actually be C# compliant, a property must take up no space at all, but yours does, unfortunately. Since you're embedding an object inside another object, I don't see how you can use this design to achieve that goal. No disrespect.

-Sid Sacek

struct driving
        double distance;
        double time;

        driving( double d, double t ) : distance( d ), time( t ) {}

        template< typename Base > class driving_statistic : public Base
                const double get() const { return self()->distance / self()->time; }

        BOOST_PROPERTY( driving, driving_statistic, speed );

//And usage:
void test()
        driving drv( 500, 10 );

        double speed = drv.speed;

        size_t sz = sizeof( driving );
        BOOST_STATIC_ASSERT( sizeof( driving ) == sizeof( double ) * 2 );

-----Original Message-----
From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]] On Behalf Of Peder Holt
Sent: Monday, October 19, 2009 1:04 PM
To: boost_at_[hidden]
Subject: [boost] [property] interest in C# like properties for C++?

I have implemented a prototype support for properties in C++.
It has no space overhead as it manages to deduce the this pointer of the class owning it.

Code sample:

#include <boost/static_assert.hpp>
#include <boost/property.hpp>
#include <iostream>

class class_with_property {
     //Implement property template
     template<typename Base>
     class property_with_a_sensible_name : public Base
        void set(const double& a) {
           //Implement setting of the variable here.
           //You can access the owner of the property's this value by calling self(), e.g.
        const double& get() const {
           return m_var;
        double m_var;
     void refresh() {
        std::cout << "Refreshed";
     //Instantiate property template. These can be reused.


//And usage:
void test() {
     class_with_property cls;

The trick used is the following:

class my_class {
  class property_x {
     my_class* self() {
        //Find offset of member relative to my_class
        size_t offset=size_t(&((my_class*)0)->x);
        //Use this to deduce the address of my_class
        return reinterpret_cast<my_class*>(size_t(this)-offset);
  property_x x;

It currently support:
Read/Write properties
Read properties
Write properties

Not supported yet:
indexed properties
operator overloads for properties, e.g.*
will currently fail.


Boost list run by bdawes at, gregod at, cpdaniel at, john at