Concept matching -- function like interfaces.

Discussion in 'C++' started by Noah Roberts, Oct 7, 2011.

  1. Noah Roberts

    Noah Roberts Guest

    So I am trying to come up with a way to do concepts in C++11. Mostly
    they can be implemented with TMP and SFINAE using decltype. For

    template < typename T, typename R, typename ... Args >
    struct can_call_fun<T,R(Args...)>
    typedef char (&yes) [1];
    typedef char (&no) [2];

    template < typename U >
    static yes check(decltype(declval<U>().fun(declval<Args>()...)) * );
    template < typename U >
    static no check(...);

    enum { value = sizeof(check<T>(0)) == sizeof(yes) };

    The above metafunction will establish that you can use the,x,x) syntax on an instance of T. I'll use another check to
    verify that you can convert the return to a specific target type. The
    purpose of this construct above and beyond simply trying to grab a
    member function pointer and checking types is that the target could be
    realizing this concept with template functions or even functor
    members. The naive member function check would fail under these
    conditions even if the type really does realize the target concept.
    This metafunction then is a step toward allowing these kinds of

    BUT...this metafunction succeeds if the types of args are convertible,
    even if doing so is unsafe and would generate a warning under some
    compile flags. If one used "-Wconversion -Werror" for example and
    passed a type to this thing expecting it to say "NO" when you pass
    doubles to a function expecting ints, you would be mistaken; on very
    recent versions of g++ that is. The concept check would then succeed
    and fail deep down inside the template instantiation, which is sort of
    the point of doing concepts to begin with.

    The fact that it succeeds when types are convertible is a good thing.
    An object that has a function taking two ints and returning void
    should probably match a concept that requires a function of the same
    name but takes shorts instead because the preconditions are more
    allowing than the concept requires. Also a function that takes the
    appropriate arguments and then additionally some default arguments
    should match. However, if the function takes shorts but the concept
    requires ints then the match seems incorrect, but this metafunction
    would claim it matches.

    I've been struggling for a few days to consider a way to check that
    coersion will not lose information. The return is easy since I can
    get that from the decltype expression and write a metafunction that
    uses a table to establish information loss. On the other hand, the
    parameters is a problem for this construct. There are some things you
    could maybe try, fail, and try another with, but when it comes to
    templates and such it seems to me that it would be damn near
    impossible to predict a signature and grab the address or whatever.

    So.... any ideas?
    Noah Roberts, Oct 7, 2011
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
  2. Negroup
    James Carroll
    Jun 23, 2005
  3. Patrick Kowalzick
    Patrick Kowalzick
    Mar 14, 2006
  4. Morten Lemvigh

    Java-like interfaces in c++

    Morten Lemvigh, Mar 30, 2007, in forum: C++
    Morten Lemvigh
    Mar 30, 2007
  5. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Marc Bissonnette
    Jan 13, 2004

Share This Page