On 16.12.2013 15:15,
hui...@gmail.com wrote:
> i don't understand how that could help...
Sorry, I don't what I was (not) thinking. :-)
Your latest code does compile with g++ 4.4, and fails with g++ 3.3.
If it doesn't compile with g++ 4.3, then the original on which I based
the following, reportedly compiles with g++ 4.1, so should be fine?
[code]
struct Yes { char x; };
struct No { char x[2*sizeof(Yes)]; };
// This type won't compile if the second template parameter isn't of type T,
// so I can put a function pointer type in the first parameter and the
function
// itself in the second thus checking that the function has a specific
signature.
template <typename T, T> struct Type_check;
// Baed on FireAphi's code at <url:
http://stackoverflow.com/a/3627243/464581>.
template <class Type>
class Has_memfun
{
// A helper struct to hold the declaration of the function pointer.
// Change it if the function signature changes.
template <typename T> struct Func_type
{
typedef void (T::*fptr)();
};
template <typename T> static Yes has_memfun(Type_check< typename
Func_type<T>::fptr, &T::memfun >*);
template <typename T> static No has_memfun(...);
public:
static bool const yes = (sizeof(has_memfun<Type>(0)) == sizeof(Yes));
};
struct X
{ void memfun(int){}; };
struct Y
{};
struct Z
{ void memfun(){}; };
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
bool const x_has = Has_memfun<X>::yes;
bool const y_has = Has_memfun<Y>::yes;
bool const z_has = Has_memfun<Z>::yes;
cout << (x_has? "X has memfun(), yay!" : "No X::memfun()") << endl;
cout << (y_has? "Y has memfun(), yay!" : "No Y::memfun()") << endl;
cout << (z_has? "Z has memfun(), yay!" : "No Z::memfun()") << endl;
}
[/code]