Determine if type supports operation at compile time

Discussion in 'C++' started by Marcin Kalicinski, Aug 2, 2007.

  1. Is there a way, using template metaprogramming techinques like e.g. SFINAE,
    to determine at compile time if a type supports certain operation (for
    example if operator == is defined for that type)?

    // Function for types supporting ==
    template<class T> bool func(T t)
    {
    if (t == 3)
    return true;
    else
    return false;
    }

    // Function for types not supporting ==
    template<class T> bool func(T t)
    {
    return false;
    }

    Can I somehow make compiler call appropriate function depending on type of
    T?

    thanks,
    Marcin
     
    Marcin Kalicinski, Aug 2, 2007
    #1
    1. Advertising

  2. Marcin Kalicinski

    kwikius Guest

    On 2 Aug, 20:47, "Marcin Kalicinski" <> wrote:
    > Is there a way, using template metaprogramming techinques like e.g. SFINAE,
    > to determine at compile time if a type supports certain operation (for
    > example if operator == is defined for that type)?
    >
    > // Function for types supporting ==
    > template<class T> bool func(T t)
    > {
    > if (t == 3)
    > return true;
    > else
    > return false;
    >
    > }
    >
    > // Function for types not supporting ==
    > template<class T> bool func(T t)
    > {
    > return false;
    >
    > }
    >
    > Can I somehow make compiler call appropriate function depending on type of
    > T?
    >
    > thanks,
    > Marcin


    Using the example of ==, there are plans to incorporate this type of
    functionality into the next version of C++.

    See the ConceptG++ EqualityComparable concept:

    http://tinyurl.com/32q2bq

    regards
    Andy Little
     
    kwikius, Aug 3, 2007
    #2
    1. Advertising

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. Ashabul Yeameen
    Replies:
    1
    Views:
    356
    Mike Wahler
    Sep 16, 2003
  2. Ashabul Yeameen
    Replies:
    2
    Views:
    451
    Ashabul Yeameen
    Sep 17, 2003
  3. Replies:
    3
    Views:
    378
    Victor Bazarov
    Nov 1, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    914
    Lionel B
    Mar 1, 2007
  5. Carter
    Replies:
    2
    Views:
    528
    Carter
    Mar 4, 2009
Loading...

Share This Page