Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #477

Lambdas as template arguments

Path csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail
From mrts.pydev@gmail.com
Newsgroups comp.std.c++
Subject Lambdas as template arguments
Date Sun, 8 Apr 2012 08:15:02 -0700 (PDT)
Organization http://groups.google.com
Lines 92
Sender std-cpp-request@vandevoorde.com
Approved stephen.clamage@oracle.com
Message-ID <11147290.10.1333796825738.JavaMail.geo-discussion-forums@vbbfj25> (permalink)
NNTP-Posting-Host C6qOEBHVmEUdgTMpJ8WQWAkZFg3zchBqzZu5VukgI90=
Content-Type text/plain; charset=windows-1252
X-Trace news.albasani.net aBYVO6GgNrxdk9pjmrSbN0gNURPpfAOH88dtB6MyIv8jeZfGkvMr47VcQ9LXt/2X/8KwqD1iYufJ176etvwzLw==
X-Complaints-To abuse@albasani.net
NNTP-Posting-Date Sun, 8 Apr 2012 15:15:04 +0000 (UTC)
X-Mailer Perl5 Mail::Internet v2.05
X-Submission-Address std-cpp-submit@vandevoorde.com
Cancel-Lock sha1:wAfaDF1O5Vp7QzBC6BspDntxUl4=
X-Original-Date Sat, 7 Apr 2012 04:07:05 -0700 (PDT)
Xref csiph.com comp.std.c++:477

Show key headers only | View raw


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 | NextNext in thread | Find similar | Unroll thread


Thread

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