interface constraints for a template

85 views
Skip to first unread message

sergeik...@gmail.com

unread,
Nov 22, 2015, 10:51:20 AM11/22/15
to ISO C++ Standard - Future Proposals
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.
   
  

Nicol Bolas

unread,
Nov 22, 2015, 10:55:28 AM11/22/15
to ISO C++ Standard - Future Proposals, sergeik...@gmail.com
We call those `concepts`.

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.

Nicol Bolas

unread,
Nov 22, 2015, 10:56:32 AM11/22/15
to ISO C++ Standard - Future Proposals, sergeik...@gmail.com

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.

Ville Voutilainen

unread,
Nov 22, 2015, 11:06:41 AM11/22/15
to ISO C++ Standard - Future Proposals
On 22 November 2015 at 17:51, <sergeik...@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

Ville Voutilainen

unread,
Nov 22, 2015, 11:15:53 AM11/22/15
to ISO C++ Standard - Future Proposals
On 22 November 2015 at 18:06, Ville Voutilainen
<ville.vo...@gmail.com> wrote:
> On 22 November 2015 at 17:51, <sergeik...@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

Сергей Кривонос

unread,
Nov 22, 2015, 11:57:14 AM11/22/15
to ISO C++ Standard - Future Proposals
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_ojhNDzfV4/nDI5F9vABQAJ my proposal as you do in this one. Because it seems was ignored but have far better feature.

вс, 22 нояб. 2015 г. в 18:15, Ville Voutilainen <ville.vo...@gmail.com>:
--

---
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-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
Reply all
Reply to author
Forward
0 new messages