Is a class type using SFINAE

S

siddhu

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
 
V

Victor Bazarov

siddhu said:
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 said:
};


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top