Is a class type using SFINAE

Discussion in 'C++' started by siddhu, May 17, 2007.

  1. siddhu

    siddhu Guest

    Dear Experts,

    I am trying to implement "Is a class type" for template arguments
    using SFINAE.

    program is like this:

    #include<iostream>
    using namespace std;
    template <typename T>
    class IsClass
    {
    private:
    typedef char one;
    typedef struct{char a[2];} two;
    template<typename C>static one test(int C::*);
    template<typename C>static two test(...);
    public:
    enum { Yes = sizeof(IsClass<T>::test<T>(0)) == 1 };
    };


    template<typename T>
    void check()
    {
    if (IsClass<T>::Yes) {
    std::cout << " Is Class" << std::endl;
    }
    else {
    std::cout << " Not Class " << std::endl;
    }

    }
    class ABCD
    {
    };

    int main()
    {
    cout<<"int";
    check<int>();
    cout<<"ABCD";
    check<ABCD>();
    }

    It does not compile on g++ compiler.

    On CC it gives the following output:

    int Not Class
    ABCD Not Class

    But the expected behavior for class ABCD was
    ABCD Is Class.

    Suggestions would be very helpful.

    Thanks and Regards,
    Siddharth
    siddhu, May 17, 2007
    #1
    1. Advertising

  2. siddhu wrote:
    > Dear Experts,
    >
    > I am trying to implement "Is a class type" for template arguments
    > using SFINAE.
    >
    > program is like this:
    >
    > #include<iostream>
    > using namespace std;
    > template <typename T>
    > class IsClass
    > {
    > private:
    > typedef char one;
    > typedef struct{char a[2];} two;
    > template<typename C>static one test(int C::*);
    > template<typename C>static two test(...);
    > public:
    > enum { Yes = sizeof(IsClass<T>::test<T>(0)) == 1 };


    Visual C++ compiles it and gives the expected behaviour if you
    remove the 'IsClass<T>::' from the expression inside sizeof:

    enum { Yes = sizeof(test<T>(0)) == 1 };

    > };
    >
    >
    > template<typename T>
    > void check()
    > {
    > if (IsClass<T>::Yes) {
    > std::cout << " Is Class" << std::endl;
    > }
    > else {
    > std::cout << " Not Class " << std::endl;
    > }
    >
    > }
    > class ABCD
    > {
    > };
    >
    > int main()
    > {
    > cout<<"int";
    > check<int>();
    > cout<<"ABCD";
    > check<ABCD>();
    > }
    >
    > It does not compile on g++ compiler.
    >
    > On CC it gives the following output:
    >
    > int Not Class
    > ABCD Not Class
    >
    > But the expected behavior for class ABCD was
    > ABCD Is Class.
    >
    > Suggestions would be very helpful.


    If after proposed changes it still doesn't work, contact GCC
    people.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 17, 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. Peter Collingbourne

    problem with SFINAE applied to class methods

    Peter Collingbourne, Jul 1, 2004, in forum: C++
    Replies:
    8
    Views:
    443
    Pete Becker
    Jul 4, 2004
  2. Clark S. Cox III

    Using SFINAE with constructors

    Clark S. Cox III, Sep 9, 2005, in forum: C++
    Replies:
    2
    Views:
    365
    Howard Hinnant
    Sep 9, 2005
  3. Fei Liu
    Replies:
    5
    Views:
    470
    Barry
    May 29, 2008
  4. Barry
    Replies:
    2
    Views:
    342
    Fei Liu
    May 29, 2008
  5. Victor Bogado
    Replies:
    3
    Views:
    453
    James Kanze
    Mar 3, 2011
Loading...

Share This Page