Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-24-2008, 12:58 AM
Kaba
 
Posts: n/a
Emulating concept based overloading

Hi,

Today I found a way to emulate concept based overloading in a specific
case in C++03. As always, it has probably been found before by many, but
I was so excited that I had to write it down:) The essence is the
following. We start from this:

template <typename Input_ConstView2, typename Output_View2>
void copy(
const Input_ConstView2& input,
const Output_View2& output)
{
// Copy..
}

And end up with this:

template <
typename Element,
typename Input_ConstView2,
typename Output_View2>
void copy(
const ConstView2<Element, Input_ConstView2>& input,
const View2<Element, Output_View2>& output)
{
// Copy...
}

The former is an unrestricted template, while the latter matches only
specific classes. However, the classes can contain any type that models
the concepts (View2 and ConstView2) and they itself model the same
concepts. Thus this kind of emulates concept based overloading. The
technique can also be used to recover associated types of concepts to
template parameter level, which can be quite useful. This technique
works in this specific case where the views are of small size.

If you have the time, have a look at the full text at:

http://kaba.hilvi.org/Programming_C+...t_Wrapping.htm

Comments?

--
http://kaba.hilvi.org

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-28-2008, 09:16 PM
Mathias Gaunard
 
Posts: n/a
Re: Emulating concept based overloading

On 24 août, 01:58, Kaba <n...@here.com> wrote:

> The former is an unrestricted template, while the latter matches only
> specific classes. However, the classes can contain any type that models
> the concepts (View2 and ConstView2) and they itself model the same
> concepts. Thus this kind of emulates concept based overloading.


Concepts are structural subtyping and are perfectly implicit.
I fail to see how your approach does it. It is nothing but an explicit
subtyping technique.

It is perfectly possible to implement real concept-like overloading in
C++03, though, via compile-time reflection, with has a few limitations
(not possible to detect operator= and constructors).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-29-2008, 05:38 AM
Kaba
 
Posts: n/a
Re: Emulating concept based overloading

Mathias Gaunard wrote:
> On 24 août, 01:58, Kaba <n...@here.com> wrote:
>
> > The former is an unrestricted template, while the latter matches only
> > specific classes. However, the classes can contain any type that models
> > the concepts (View2 and ConstView2) and they itself model the same
> > concepts. Thus this kind of emulates concept based overloading.

>
> Concepts are structural subtyping and are perfectly implicit.
> I fail to see how your approach does it. It is nothing but an explicit
> subtyping technique.


That's right. An object of a class that models the concept is explicitly
wrapped up in an object of a specific class for the purposes of
overloading. The similarities to concept based overloading are that:

1) An existing function that has been written for the wrapped classes
works automatically for a newly created class modeling the concept
(given that it has been wrapped).

2) Everytime the existing function in 1) matches a call, we can be sure
that the types of the arguments model the right concepts.

Do you mean to say that the technique is not similar to concept based
overloading at all? If so, please elaborate.

Btw, this technique is also similar to what is possible with the
curiously recurring template pattern. With expression templates (for
vectors in R^n, for example) one can use crtp to enable something like
this:

//! Returns the sum of elements.

template <int N, typename Real, typename Expression>
Real sum(const VectorExpression<N, Real, Expression>& x);

However, when using the crtp, one has to transmit all the needed type
information from the derived class to the superclass via the template
parameters. In the case of the View2 example the views have inner cursor
(iterator) classes which would need too much typing for me:

// Crtp approach

template <typename Derived_Element, typename Derived_View2, typename
Derived_ConstCursor, typename Derived_Cursor>
class View2
{
public:
typedef Derived_ConstCursor ConstCursor;
typedef Derived_Cursor Cursor;
//...
};

Thus I ended up in a wrapping approach instead:

template <typename Contained_Element, typename Contained_View2>
class View2
{
public:
typedef typename Contained_View2::ConstCursor ConstCursor;
typedef typename Contained_View2::Cursor Cursor;
//...
};

However, in exchange for the reduction in template parameters (the
Contained_Element was intentionally left as a parameter), I now need to
explicitly wrap the object to an object of a class of the ConstView2
family. That is not problematic in my case: the produced types from
views in views quickly become so complex anyway that it is unpractical
to write their types down (I am anxiously waiting for the automatic type
deduction in C++0x :). I avoid this by using template functions to
produce those temporary view objects with complex types. The wrapping
then fits there transparently.

> It is perfectly possible to implement real concept-like overloading in
> C++03, though, via compile-time reflection, with has a few limitations
> (not possible to detect operator= and constructors).


Define what you mean by _real_ concept-like overloading. How does that
technique work?

--
http://kaba.hilvi.org


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-30-2008, 03:44 PM
Mathias Gaunard
 
Posts: n/a
Re: Emulating concept based overloading

On 29 août, 06:38, Kaba <n...@here.com> wrote:

> Define what you mean by _real_ concept-like overloading. How does that
> technique work?


With meta-functions that check whether a type fulfills certain
criteria using compile-time reflection techniques and SFINAE to do the
overloading.

See http://neoscientists.org/~tschwinger...pt_traits/doc/
for an implementation.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #5 (permalink)  
Old 08-30-2008, 03:58 PM
David Abrahams
 
Posts: n/a
Re: Emulating concept based overloading


on Thu Aug 28 2008, Mathias Gaunard <loufoque-AT-gmail.com> wrote:

> On 24 août, 01:58, Kaba <n...@here.com> wrote:
>
>> The former is an unrestricted template, while the latter matches only
>> specific classes. However, the classes can contain any type that models
>> the concepts (View2 and ConstView2) and they itself model the same
>> concepts. Thus this kind of emulates concept based overloading.

>
> Concepts are structural subtyping and are perfectly implicit.


Actually, that's not quite true. Don't forget that concepts have
semantic requirements that can't be deduced by the compiler. There many
input iterators that look, structurally, exactly like forward
iterators. If you don't do something explicit to distinguish them, it
has severe consequences. Try

std::vector<int>(b, e);

on your favorite standard library where b and e are mis-labeled input
iterators, and you'll see why. Expect a crash or worse. That's part of
why concepts in C++0x are not "auto" by default. From my P.O.V., having
to explicitly state concept conformance is the norm.

> I fail to see how your approach does it. It is nothing but an explicit
> subtyping technique.


I haven't looked at the code, but IMO there are two things required to
make it an interesting technique from a Generic Programming point of view:

* it would have to be *non-intrusive*, so you could establish
conformance of a type T to a concept X without altering T or X.

* it would have to be able to establish conformance of all instances of
a template to a given concept, i.e., "all vector<T> instances model
RandomAccessContainer."

--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #6 (permalink)  
Old 09-01-2008, 11:03 PM
Kaba
 
Posts: n/a
Re: Emulating concept based overloading

Mathias Gaunard wrote:
> On 29 août, 06:38, Kaba <n...@here.com> wrote:
>
> > Define what you mean by _real_ concept-like overloading. How does that
> > technique work?

>
> With meta-functions that check whether a type fulfills certain
> criteria using compile-time reflection techniques and SFINAE to do the
> overloading.
>
> See http://neoscientists.org/~tschwinger...pt_traits/doc/
> for an implementation.


To moderators: I'm a bit irritated by your manner to cencor any "thank
you"-messages in this forum (this has happened to me several times in
here). In my opinion, those are not superfluous but essential in forming
a discussion that has a start, a middle, and an end. Cencoring the
message that brings the thread to a closure on the part of the OP sends
the thread in a middle-limbo and gives an impression of the OP ignoring
the replies. What I wish to be able to communicate through with this
kind of post is that I have read and understood the reply.

{ In short, [thank you] is a rejection reason documented in the charter.
Discussion about the moderation policy is always on-topic. -mod/sk }

--
http://kaba.hilvi.org


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #7 (permalink)  
Old 09-02-2008, 08:43 PM
Jiang
 
Posts: n/a
Re: Emulating concept based overloading

On Sep 2, 7:03 am, Kaba <n...@here.com> wrote:

[...]

> To moderators: I'm a bit irritated by your manner to cencor any "thank
> you"-messages in this forum (this has happened to me several times in
> here). In my opinion, those are not superfluous but essential in forming
> a discussion that has a start, a middle, and an end. Cencoring the
> message that brings the thread to a closure on the part of the OP sends
> the thread in a middle-limbo and gives an impression of the OP ignoring
> the replies. What I wish to be able to communicate through with this
> kind of post is that I have read and understood the reply.
>


As a rule of thumb, you can always solve this problem by writing a
short summary for your discussion, and please do use some C++
keywords to express your points. After that, attach your "thank you"
message and everything will be fine. ;-)

Of course, this also helps for other who read your discussion later.

> { In short, [thank you] is a rejection reason documented in the charter.
> Discussion about the moderation policy is always on-topic. -mod/sk }


Hope this post is "on-topic", and I agree with you that post only
contains
"thank you" should always be rejected. Thank you for your time.

Regards,

Jiang


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #8 (permalink)  
Old 09-03-2008, 09:58 PM
Mathias Gaunard
 
Posts: n/a
Re: Emulating concept based overloading

On 30 août, 16:58, David Abrahams <d...@boostpro.com> wrote:

> > Concepts are structural subtyping and are perfectly implicit.

>
> Actually, that's not quite true. Don't forget that concepts have
> semantic requirements that can't be deduced by the compiler. [...]
> That's part of
> why concepts in C++0x are not "auto" by default. From my P.O.V., having
> to explicitly state concept conformance is the norm.


That's true, indeed.
But emulating non-auto concept-like behaviour doesn't seem like a real
challenge to me (SFINAE and traits have done that for a long time
already), so when I read of emulating concepts I mostly thought of
auto ones.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:06 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Electricity Suppliers | Mortgage Calculator | Repair Bad Credit | Online Degrees | Per Insurance



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114