V
Vladimir Nesterovsky
Hello,
I have a code which behaves differently if template parameter has a
specified member:
#include <iostream>
template<class T, bool (T::*)()>
class member_bind
{
public:
typedef T type;
};
template<class T1, class T2>
class has_member
{
public:
static const bool value = false;
};
// 1
template<class T>
class has_member<T, typename member_bind<T, T::member>::type>
{
public:
static const bool value = true;
};
class A
{
public:
bool member();
};
class B {};
int main(int argc, char* argv[])
{
std::cout << "A has member? " << (has_member<A, A>::value ? "yes" :
"no") << std::endl;
std::cout << "B has member? " << (has_member<B, B>::value ? "yes" :
"no") << std::endl;
return 0;
}
I've been able to compile only this code variation. In my opinion, however,
the following lines ain't strictly correct:
// 1
template<class T>
class has_member<T, typename member_bind<T, T::member>::type>
They should be written as:
template<class T>
class has_member<T, typename member_bind<T, &T::member>::type>
or maybe even without "typename" keyword (member_bind is known template and
have no uncertainties) as:
template<class T>
class has_member<T, member_bind<T, &T::member>::type>
Is it a bug in compiler?
I have a code which behaves differently if template parameter has a
specified member:
#include <iostream>
template<class T, bool (T::*)()>
class member_bind
{
public:
typedef T type;
};
template<class T1, class T2>
class has_member
{
public:
static const bool value = false;
};
// 1
template<class T>
class has_member<T, typename member_bind<T, T::member>::type>
{
public:
static const bool value = true;
};
class A
{
public:
bool member();
};
class B {};
int main(int argc, char* argv[])
{
std::cout << "A has member? " << (has_member<A, A>::value ? "yes" :
"no") << std::endl;
std::cout << "B has member? " << (has_member<B, B>::value ? "yes" :
"no") << std::endl;
return 0;
}
I've been able to compile only this code variation. In my opinion, however,
the following lines ain't strictly correct:
// 1
template<class T>
class has_member<T, typename member_bind<T, T::member>::type>
They should be written as:
template<class T>
class has_member<T, typename member_bind<T, &T::member>::type>
or maybe even without "typename" keyword (member_bind is known template and
have no uncertainties) as:
template<class T>
class has_member<T, member_bind<T, &T::member>::type>
Is it a bug in compiler?