> Excellent solution, thanks for sharing this.

Thanks, I got excellent help

.

But unfortunately, the solution is still unusable for me as it strangely doesn't work any longer if the functor uses the Curiously Recurring Template Pattern, which I need in my real implementation.

Below is a sample implementation showing the issue: As long as SMART isn't defined, the initial "dumb" implementation is used and everything compiles fine even when CRTP is involved, but using the new, "smart" solution, I getthe following compiler errors:

noncopyFunctorOrFunction.cpp: In instantiation of »isFunctionPointer<X>«:

noncopyFunctorOrFunction.cpp:25:5: instantiated from »Reference<X>«

noncopyFunctorOrFunction.cpp:41:31: instantiated from here

noncopyFunctorOrFunction.cpp:5:14: Error: »isFunctionPointer<X>::t« hasincomplete type

noncopyFunctorOrFunction.cpp:41:8: Error: forward declaration of »struct X«

#include <iostream>

template<typename T> struct isFunctionPointer {

template<typename U> static char is_ptr(U (*)());

static double is_ptr(...);

static T t;

enum {value = sizeof(is_ptr(t)) == sizeof(char)};

};

template<typename T, bool = isFunctionPointer<T>::value> struct Type {typedef T & U;};

template<typename T> struct Type<T, true> {typedef T U;};

// A function.

void function() {std::cout << "function" << std::endl;}

// A functor.

class Functor {

public:

Functor() {}

void operator()() {std::cout << "functor" << std::endl;}

private:

Functor(const Functor &);

} functor;

//#define SMART

#ifdef SMART

// Unfortunately, this does not work together with

// the Curiously Recurring Template Pattern ....

template<typename T> struct Reference {

Reference(typename Type<T>::U t):f(t) {}

typename Type<T>::U f;

};

#else

// ... while the simple-minded approach still works fine.

template<typename T> struct Reference {

Reference(T &t):f(t) {}

T &f;

};

// Partial specialization of struct Reference for

// (function) pointers).

template<typename T> struct Reference<T *> {

Reference(T t):f(t) {}

T *f;

};

#endif

struct X: public Reference<X> {

X(): Reference<X>(*this) {}

void operator()() {std::cout << "functor with CRTP" << std::endl;}

} x;

// Test if it works.

int main() {

Reference<void (*)()> rf(function);

Reference<Functor> rF(functor);

rf.f();

rF.f();

Reference<X> rX(x);

rX.f();

return 0;

}

Any ideas what's going wrong and how to solve it?

Chris