Boost logo

Boost :

Subject: Re: [boost] [property] interest in C# like properties for C++?
From: David Brownstein (dbrownstein_at_[hidden])
Date: 2009-10-20 20:07:19

I've been thinking about adding properties to C++ for some time, and I've
written several different Property<T> classes, so I've been following this
discussion with great interest. IMO C# properties are not as useful as they
should be; I think that ideally a property is a declarative expression that
automatically generates getter and/or setter code.

For example I've been experimenting with a syntax that looks like this:

struct Example
        explicit Example( std::string& strName):
                name( strName ),
                count( 0 )

        Property< std::string, public_get, private_set> name;
        Property< int, public_get, public_set> count;

Now I should be able to write:

void SomeFunc( std::string& strValue )
        Example x( strValue );
        x.count = 5;
        int I = x.count;
        std::string strName =;

The point to this exercise is that as a developer I want to write less code,
not more. Here I see an opportunity to build a system to generate code for
me. So rather than emulate C# properties, I'm hoping that C++ can come up
with something better.


-----Original Message-----
From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]]
On Behalf Of Stefan Strasser
Sent: Tuesday, October 20, 2009 1:30 PM
To: boost_at_[hidden]
Subject: Re: [boost] [property] interest in C# like properties for C++?

Am Tuesday 20 October 2009 22:07:28 schrieb Matthew Chambers:
> Do you plan to have a macro for defining simple/trivial properties with
> a single line?

I think that this is the only thing that's missing from c++, and that it
doesn't need a C# properties syntax.

the C++ "properties" syntax is, for example (taken from tr1::unordered_map):

float max_load_factor() const;
void max_load_factor(float z);

I wouldn't even consider your proposed properties syntax "syntactic sugar".
it's just a different syntax, not a better one imho.

so if there's anything missing from c++ it would be something like

  T name() const{ return _name; } \
  void name(T __name){ _name=__name; } \
  T _name;

some of the problems your syntax introduces have already been mentioned.
another one is that the getters and setters can not be virtual. C# and
C++/CLI allow overriding setters and getters, and pure virtual properties,
but I don't think that can be achieved with the same syntax in C++, since
setters/getters are functions of seperate nested classes.
Unsubscribe & other changes:

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