Topic: interface constraints for a template


Author: sergeikrivonos@gmail.com
Date: Sun, 22 Nov 2015 07:51:20 -0800 (PST)
Raw View
------=_Part_2422_1428641287.1448207480850
Content-Type: multipart/alternative;
 boundary="----=_Part_2423_84892755.1448207480850"

------=_Part_2423_84892755.1448207480850
Content-Type: text/plain; charset=UTF-8

Make ability to specify a class from which the template should be inherited
of.

class I {
   virtual int IMethod();
};

class II {
    virtual bool IMethod();
};

template <class T, class IT : I> // here we specify that T should be
inherited from I (directly/undirectly/castable - under consideration (TBD))
void T fn(const IT& t) {
    return t.IMethod();
}

This gives us benefits for IDE (development tools) ability to make code
suggestions. CTRL+SPACE after 't.' can give us IMethod in proposals.



--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_2423_84892755.1448207480850
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Make ability to specify a class from which the template sh=
ould be inherited of.<br><br>class I {<br>=C2=A0=C2=A0 virtual int IMethod(=
);<br>};<br><br>class II {<br>=C2=A0=C2=A0=C2=A0 virtual bool IMethod();<br=
>};<br><br>template &lt;class T, class IT : I&gt; // here we specify that T=
 should be inherited from I (directly/undirectly/castable - under considera=
tion (TBD))<br>void T fn(const IT&amp; t) {<br>=C2=A0=C2=A0=C2=A0 return t.=
IMethod();<br>}<br><br>This gives us benefits for IDE (development tools) a=
bility to make code suggestions. CTRL+SPACE after &#39;t.&#39; can give us =
IMethod in proposals.<br>=C2=A0=C2=A0=C2=A0 <br>=C2=A0=C2=A0 <br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_2423_84892755.1448207480850--
------=_Part_2422_1428641287.1448207480850--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sun, 22 Nov 2015 07:55:28 -0800 (PST)
Raw View
------=_Part_216_780705421.1448207728506
Content-Type: multipart/alternative;
 boundary="----=_Part_217_1329632678.1448207728506"

------=_Part_217_1329632678.1448207728506
Content-Type: text/plain; charset=UTF-8

We call those `concepts
<http://en.cppreference.com/w/cpp/language/constraints>`.

However, I think it's silly to constrain a function based on a base class.
Constrain it based on the *interface* provided by that base class. Why does
it matter if the user derived from that class or not? What matters is
whether the class they provide offers the proper interface?

If you wanted to constrain the parameter on a base class, you should take
the base class itself as a parameter and not use a template parameter at
all.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_217_1329632678.1448207728506
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">We call those `<a href=3D"http://en.cppreference.com/w/cpp=
/language/constraints">concepts</a>`.<br><br>However, I think it&#39;s sill=
y to constrain a function based on a base class. Constrain it based on the =
<i>interface</i> provided by that base class. Why does it matter if the use=
r derived from that class or not? What matters is whether the class they pr=
ovide offers the proper interface?<br><br>If you wanted to constrain the pa=
rameter on a base class, you should take the base class itself as a paramet=
er and not use a template parameter at all.<br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_217_1329632678.1448207728506--
------=_Part_216_780705421.1448207728506--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sun, 22 Nov 2015 07:56:32 -0800 (PST)
Raw View
------=_Part_53_1034410841.1448207792197
Content-Type: multipart/alternative;
 boundary="----=_Part_54_704085495.1448207792197"

------=_Part_54_704085495.1448207792197
Content-Type: text/plain; charset=UTF-8



On Sunday, November 22, 2015 at 10:55:28 AM UTC-5, Nicol Bolas wrote:
>
> We call those `concepts
> <http://en.cppreference.com/w/cpp/language/constraints>`.
>
> However, I think it's silly to constrain a function based on a base class.
> Constrain it based on the *interface* provided by that base class. Why
> does it matter if the user derived from that class or not? What matters is
> whether the class they provide offers the proper interface?
>
> If you wanted to constrain the parameter on a base class, you should take
> the base class itself as a parameter and not use a template parameter at
> all.
>

Not to mention, you're using a *virtual function*. The whole point of that
is that you can call it from a base class pointer and get a call to the
method in the derived class. Using template polymorphism on top of virtual
polymorphism makes no sense.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_54_704085495.1448207792197
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<br><br>On Sunday, November 22, 2015 at 10:55:28 AM UTC-5, Nicol Bolas wrot=
e:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;b=
order-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">We call tho=
se `<a href=3D"http://en.cppreference.com/w/cpp/language/constraints" targe=
t=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.go=
ogle.com/url?q\75http%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Flanguage%2Fco=
nstraints\46sa\75D\46sntz\0751\46usg\75AFQjCNFpVVvWH2Yf7orQMnYg0VVlbsWVpw&#=
39;;return true;" onclick=3D"this.href=3D&#39;http://www.google.com/url?q\7=
5http%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Flanguage%2Fconstraints\46sa\7=
5D\46sntz\0751\46usg\75AFQjCNFpVVvWH2Yf7orQMnYg0VVlbsWVpw&#39;;return true;=
">concepts</a>`.<br><br>However, I think it&#39;s silly to constrain a func=
tion based on a base class. Constrain it based on the <i>interface</i> prov=
ided by that base class. Why does it matter if the user derived from that c=
lass or not? What matters is whether the class they provide offers the prop=
er interface?<br><br>If you wanted to constrain the parameter on a base cla=
ss, you should take the base class itself as a parameter and not use a temp=
late parameter at all.<br></div></blockquote><div><br>Not to mention, you&#=
39;re using a <i>virtual function</i>. The whole point of that is that you =
can call it from a base class pointer and get a call to the method in the d=
erived class. Using template polymorphism on top of virtual polymorphism ma=
kes no sense.<br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_54_704085495.1448207792197--
------=_Part_53_1034410841.1448207792197--

.


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Sun, 22 Nov 2015 18:06:39 +0200
Raw View
On 22 November 2015 at 17:51,  <sergeikrivonos@gmail.com> wrote:
> Make ability to specify a class from which the template should be inherited
> of.

We already have that ability, by constraining via std::is_base_of.

>
> class I {
>    virtual int IMethod();
> };
>
> class II {
>     virtual bool IMethod();
> };
>
> template <class T, class IT : I> // here we specify that T should be
> inherited from I (directly/undirectly/castable - under consideration (TBD))
> void T fn(const IT& t) {
>     return t.IMethod();
> }

In other words,

#include <type_traits>

template <class D, class B> concept bool Derives()
{
    return std::is_base_of<B, D>::value;
}

class I {
public:
    virtual int IMethod() const = 0;
};

template <class T, class IT>
T fn(const IT& t) requires Derives<IT, I>() {
    return t.IMethod();
}

class CT : public I
{
public:
    int IMethod() const {return 42;}
};

int main()
{
    CT ct;
    fn<int>(ct);
}

See live demo here http://melpon.org/wandbox/permlink/zYSSJ8yK6GRLYxgf

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Sun, 22 Nov 2015 18:15:50 +0200
Raw View
On 22 November 2015 at 18:06, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
> On 22 November 2015 at 17:51,  <sergeikrivonos@gmail.com> wrote:
>> Make ability to specify a class from which the template should be inherited
>> of.
>
> We already have that ability, by constraining via std::is_base_of.

And if you want, you can do it in C++11, to make even msvc grok it:

#include <type_traits>

class I {
public:
    virtual int IMethod() const = 0;
};

template <class IT, typename = typename
std::enable_if<std::is_base_of<I, IT>::value>::type>
auto fn(const IT& t) -> decltype(t.IMethod()) {
    return t.IMethod();
}

class CT : public I
{
public:
    int IMethod() const {return 42;}
};

int main()
{
    CT ct;
    fn(ct);
}

Live demo: http://melpon.org/wandbox/permlink/v9cwF2PnFu03FQEW

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.


Author: =?UTF-8?B?0KHQtdGA0LPQtdC5INCa0YDQuNCy0L7QvdC+0YE=?= <sergeikrivonos@gmail.com>
Date: Sun, 22 Nov 2015 16:57:02 +0000
Raw View
--047d7bb70892fb5f54052523fffb
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

well this example is not as good to show the feature point because its
point in its simplicity. Te point I described in verbs. Thank you very much
for good answer. Concept are good but verbose. My suggestion is coming from
generics of Java and C# where it allows IDE to make good code suggestions.
I want to note that development tools support is the one of criteria of
selecting C++ features to the standard. I wish someone could be as good in
commenting of this
https://groups.google.com/a/isocpp.org/forum/#!msg/std-proposals/o_ojhNDzfV=
4/nDI5F9vABQAJ
my proposal as you do in this one. Because it seems was ignored but have
far better feature.

=D0=B2=D1=81, 22 =D0=BD=D0=BE=D1=8F=D0=B1. 2015 =D0=B3. =D0=B2 18:15, Ville=
 Voutilainen <ville.voutilainen@gmail.com
>:

> On 22 November 2015 at 18:06, Ville Voutilainen
> <ville.voutilainen@gmail.com> wrote:
> > On 22 November 2015 at 17:51,  <sergeikrivonos@gmail.com> wrote:
> >> Make ability to specify a class from which the template should be
> inherited
> >> of.
> >
> > We already have that ability, by constraining via std::is_base_of.
>
> And if you want, you can do it in C++11, to make even msvc grok it:
>
> #include <type_traits>
>
> class I {
> public:
>     virtual int IMethod() const =3D 0;
> };
>
> template <class IT, typename =3D typename
> std::enable_if<std::is_base_of<I, IT>::value>::type>
> auto fn(const IT& t) -> decltype(t.IMethod()) {
>     return t.IMethod();
> }
>
> class CT : public I
> {
> public:
>     int IMethod() const {return 42;}
> };
>
> int main()
> {
>     CT ct;
>     fn(ct);
> }
>
> Live demo: http://melpon.org/wandbox/permlink/v9cwF2PnFu03FQEW
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/wMfdGyQh5ZE/=
unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

--047d7bb70892fb5f54052523fffb
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">well this example is not as good to show the feature point=
 because its point in its simplicity. Te point I described in verbs. Thank =
you very much for good answer. Concept are good but verbose. My suggestion =
is coming from generics of Java and C# where it allows IDE to make good cod=
e suggestions. I want to note that development tools support is the one of =
criteria of selecting C++ features to the standard. I wish someone could be=
 as good in commenting of this <a href=3D"https://groups.google.com/a/isocp=
p.org/forum/#!msg/std-proposals/o_ojhNDzfV4/nDI5F9vABQAJ">https://groups.go=
ogle.com/a/isocpp.org/forum/#!msg/std-proposals/o_ojhNDzfV4/nDI5F9vABQAJ</a=
> my proposal as you do in this one. Because it seems was ignored but have =
far better feature.<br></div><br><div class=3D"gmail_quote"><div dir=3D"ltr=
">=D0=B2=D1=81, 22 =D0=BD=D0=BE=D1=8F=D0=B1. 2015 =D0=B3. =D0=B2 18:15, Vil=
le Voutilainen &lt;<a href=3D"mailto:ville.voutilainen@gmail.com">ville.vou=
tilainen@gmail.com</a>&gt;:<br></div><blockquote class=3D"gmail_quote" styl=
e=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 22 N=
ovember 2015 at 18:06, Ville Voutilainen<br>
&lt;<a href=3D"mailto:ville.voutilainen@gmail.com" target=3D"_blank">ville.=
voutilainen@gmail.com</a>&gt; wrote:<br>
&gt; On 22 November 2015 at 17:51,=C2=A0 &lt;<a href=3D"mailto:sergeikrivon=
os@gmail.com" target=3D"_blank">sergeikrivonos@gmail.com</a>&gt; wrote:<br>
&gt;&gt; Make ability to specify a class from which the template should be =
inherited<br>
&gt;&gt; of.<br>
&gt;<br>
&gt; We already have that ability, by constraining via std::is_base_of.<br>
<br>
And if you want, you can do it in C++11, to make even msvc grok it:<br>
<br>
#include &lt;type_traits&gt;<br>
<br>
class I {<br>
public:<br>
=C2=A0 =C2=A0 virtual int IMethod() const =3D 0;<br>
};<br>
<br>
template &lt;class IT, typename =3D typename<br>
std::enable_if&lt;std::is_base_of&lt;I, IT&gt;::value&gt;::type&gt;<br>
auto fn(const IT&amp; t) -&gt; decltype(t.IMethod()) {<br>
=C2=A0 =C2=A0 return t.IMethod();<br>
}<br>
<br>
class CT : public I<br>
{<br>
public:<br>
=C2=A0 =C2=A0 int IMethod() const {return 42;}<br>
};<br>
<br>
int main()<br>
{<br>
=C2=A0 =C2=A0 CT ct;<br>
=C2=A0 =C2=A0 fn(ct);<br>
}<br>
<br>
Live demo: <a href=3D"http://melpon.org/wandbox/permlink/v9cwF2PnFu03FQEW" =
rel=3D"noreferrer" target=3D"_blank">http://melpon.org/wandbox/permlink/v9c=
wF2PnFu03FQEW</a><br>
<br>
--<br>
<br>
---<br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/wMfdGyQh5ZE/unsubscribe" rel=3D"noreferr=
er" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/topic/std-pr=
oposals/wMfdGyQh5ZE/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org" target=3D"_blank">std-pr=
oposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" rel=3D"noreferrer" target=3D"_blank">http://groups.google.c=
om/a/isocpp.org/group/std-proposals/</a>.<br>
</blockquote></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--047d7bb70892fb5f54052523fffb--

.