Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | mrts.pydev@gmail.com |
|---|---|
| Newsgroups | comp.std.c++ |
| Subject | Lambdas as template arguments |
| Date | 2012-04-08 08:15 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <11147290.10.1333796825738.JavaMail.geo-discussion-forums@vbbfj25> (permalink) |
As far as I know, it is not currently possible to use labdas
as template arguments as there is no type that can be used
for representing them properly in template declarations.
Allowing lambdas as template arguments would be consistent
with the use of ordinary functions as template arguments.
This would open up many interesting possibilities – to name
one, implementing properties (an oft-requested feature for
C++) in expressive way would be trivial:
------------------------------------------------
struct S
{
std::property<int,
// getter
[] (int& value) { log(std::default_get(value)); },
// setter, assignment not possible if omitted
[] (int& value, const int& rhs) { log(std::default_set(value, rhs)); }>
some_field;
};
------------------------------------------------
Here's a sample implementation where I use std::function as
a placeholder for the missing template-parameter-enabled
function type (does obviously not work as of now).
------------------------------------------------
template<typename T>
const T& default_get(T& value)
{ return value; }
template<typename T>
const T& default_set(T& value, const T& rhs)
{ return value = rhs; }
template<typename T,
std::function<const T& (T&)> get = default_get<T>,
std::function<T& (T&, const T&)> set = default_set<T>>
class property
{
public:
operator const T& () const
{ return get(_value); }
T& operator= (const T& rhs)
{ return set(_value, rhs); }
private:
T _value;
};
------------------------------------------------
Note that the following is already possible with plain old
functions:
------------------------------------------------
template<typename T,
const T& (*get)(const T&) = default_get<T>,
T& (*set)(T&, const T&) = default_set<T> >
class property
{
public:
operator const T& () const
{ return get(_value); }
T& operator= (const T& rhs)
{ return set(_value, rhs); }
private:
T _value;
};
------------------------------------------------
Feedback much appreciated,
best,
Mart Sõmermaa
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to comp.std.c++ | Previous | Next — Next in thread | Find similar
Lambdas as template arguments mrts.pydev@gmail.com - 2012-04-08 08:15 -0700 Re: Lambdas as template arguments Daniel Krügler<daniel.kruegler@googlemail.com> - 2012-04-10 15:01 -0700
csiph-web