2015-10-11 42 views
0

下面代码中的注释行不会编译,因为F类型不符合专业化。有人能解释为什么吗?成员函数指针作为模板参数的问题

#include <memory> 
#include <functional> 
#include <map> 
#include <tuple> 

template <typename R, typename T, typename... Args> 
std::function<R(Args...)> memoizeMemberFunction (R T::*f(Args...), const T* t) { 
    auto cache = std::make_shared<std::map<std::tuple<const T*, Args...>, R>>(); 
    return ([f, cache](T* t, Args... args) { 
     const std::tuple<const T*, Args...> tuple(t, args...); 
     if (cache->find(tuple) == cache->end()) 
      (*cache)[tuple] = (t->*f)(args...); // Insert 'tuple' as a new key in the map *cache. 
     return (*cache)[tuple]; 
    }); 
} 

template <typename Class, typename Fptr, Fptr> struct MemberFunctionMemoizer; 

template <typename Class, typename R, typename... Args, R Class::*F(Args...)> 
struct MemberFunctionMemoizer<Class, R (Class::*)(Args...) const, F> { 
    static std::function<R(Class*, Args...)>& get (const Class* p) { 
     static std::function<R (Args...)> memoizedF (memoizeMemberFunction(F, p)); 
     return memoizedF; 
    } 
}; 

struct FibonacciCalculator { 
    unsigned long calculate(unsigned num) const { 
     using F = MemberFunctionMemoizer<FibonacciCalculator, 
      unsigned long (FibonacciCalculator::*)(unsigned) const, &FibonacciCalculator::calculate>; 
//  return (num < 2) ? num : F::get(this)(num - 1) + F::get(this)(num - 2); 
     // Won't compile because F does not meet the specialization. 
    } 
}; 

#include <iostream> 

int main() { 
    FibonacciCalculator fib; 
    std::cout << fib.calculate(10) << '\n'; 
} 

我在这里错过了什么吗?如何获得F以满足专业化?我试图从图片中删除const修饰符,但同样的问题仍然存在。

我也想维护使用成员函数指针作为模板参数的设计,即使通过使用非成员函数指针有解决此特定问题的方法。

+0

常量性。成员函数指针模板参数不是const限定的。另外,我认为'R Class :: * F(Args ...)'不是指向成员函数的指针。 – dyp

+0

我试着调整const,包括使'calculate()'非const,并且仍然得到相同的问题。如何解决这个问题? – prestokeys

+0

我觉得'R Class :: * F(Args ...)'是一个函数,它返回一个指向成员的指针,即'auto F(Args ...) - > R Class :: *'。 OTOH,'R(Class :: * F)(Args ...)'是一个指向成员函数的指针,可以是const限定的。 – dyp

回答

0

你拥有的最大的问题是你想memoize的签名的函数的事实:

unsigned long calculate(); 

,但你与num - 1num - 2调用记忆化高速缓存的返回功能F::get(this)

换句话说:memoization依赖于“使用相同参数调用的函数产生相同的返回值”这一事实,但是您正在记忆的函数不带任何参数(本身没有任何错误),但是您应该也不会传递任何参数。

您当前的FibonacciCalculator类无法使用memoization,因为它目前已实施。

我已经实现了那种可能做你想做的事情;它计算斐波那契数字它记忆成员函数调用... 注意:在memoizeMemberFunction()中有额外的“噪音”,但它是输出以显示它正在工作 - 您会在输出中看到函数调用被使用两次并计算只有一次。

#include <iostream> 
#include <memory> 
#include <functional> 
#include <map> 
#include <tuple> 

template <typename R, typename T, typename... Args> 
std::function<R(Args...)> memoizeMemberFunction (R (T::*f)(Args...), T* t) { 
    auto cache = std::make_shared<std::map<std::tuple<T*, Args...>, R>>(); 
    return [f, t, cache](Args... args) { 
     const std::tuple<T*, Args...> tuple(t, args...); 
     if (cache->find(tuple) == cache->end()) { 
      (*cache)[tuple] = (t->*f)(args...); // Insert 'tuple' as a new key in the map *cache. 
      std::cout << "Computed f("; 
      int dummy[sizeof...(Args)] = { (std::cout << args << ", ", 0)... }; 
      std::cout << ") = " << (*cache)[tuple] << std::endl; 
     } 
     std::cout << "Using f("; 
     int dummy[sizeof...(Args)] = { (std::cout << args << ", ", 0)... }; 
     std::cout << ") = " << (*cache)[tuple] << std::endl; 
     return (*cache)[tuple]; 
    }; 
} 

struct FibonacciCalculator { 
    unsigned long num; 
    unsigned long calculate(unsigned long n = (unsigned long)-1) { 
     static auto memoizedF (memoizeMemberFunction(&FibonacciCalculator::calculate, this)); 
     if(n==(unsigned long)-1) 
      return memoizedF(num); 
     return (n < 2) ? n : memoizedF(n-1) + memoizedF(n-2); 
    } 
}; 

int main() { 
    FibonacciCalculator fib{ 10 }; 
    std::cout << fib.calculate() << '\n'; 
} 
0

感谢DYP纠正我的语法可怕的斗争,我已经完成了我所要做的:

#include <memory> 
#include <functional> 
#include <map> 
#include <tuple> 

template <typename R, typename T, typename... Args> 
std::function<R(Args...)> memoizeMemberFunction (R (T::*f)(Args...), T* t) { 
    auto cache = std::make_shared<std::map<std::tuple<T*, Args...>, R>>(); 
    return ([f, cache, t](Args... args) { 
     const std::tuple<T*, Args...> tuple(t, args...); 
     if (cache->find(tuple) == cache->end()) 
      (*cache)[tuple] = (t->*f)(args...); // Insert 'tuple' as a new key in the map *cache. 
     return (*cache)[tuple]; 
    }); 
} 

template <typename R, typename T, typename... Args> 
std::function<R(Args...)> memoizeConstMemberFunction (R (T::*f)(Args...) const, const T* t) { 
    auto cache = std::make_shared<std::map<std::tuple<const T*, Args...>, R>>(); 
    return ([f, cache, t](Args... args) { 
     const std::tuple<const T*, Args...> tuple(t, args...); 
     if (cache->find(tuple) == cache->end()) 
      (*cache)[tuple] = (t->*f)(args...); // Insert 'tuple' as a new key in the map *cache. 
     return (*cache)[tuple]; 
    }); 
} 

template <typename Class, typename Fptr, Fptr> struct MemberFunctionMemoizer; 
template <typename Class, typename Fptr, Fptr> struct ConstMemberFunctionMemoizer; 

template <typename Class, typename R, typename... Args, R (Class::*F)(Args...) const> 
struct ConstMemberFunctionMemoizer<Class, R (Class::*)(Args...) const, F> { 
    static std::function<R(Args...)>& get (const Class* p) { 
     static std::function<R (Args...)> memoizedF (memoizeConstMemberFunction(F, p)); 
     return memoizedF; 
    } 
}; 

template <typename Class, typename R, typename... Args, R (Class::*F)(Args...)> 
struct MemberFunctionMemoizer<Class, R (Class::*)(Args...), F> { 
    static std::function<R(Args...)>& get (Class* p) { 
     static std::function<R (Args...)> memoizedF (memoizeMemberFunction(F, p)); 
     return memoizedF; 
    } 
}; 

// Testing 
#include <iostream> 
#include <vector> 

struct FibonacciCalculator { 
    std::vector<unsigned long> computed; 
    unsigned long calculate (unsigned num) const { 
     using F = ConstMemberFunctionMemoizer<FibonacciCalculator, unsigned long (FibonacciCalculator::*)(unsigned) const, &FibonacciCalculator::calculate>; // 'decltype(&FibonacciCalculator::calculate)' can be used in place of 'unsigned long (FibonacciCalculator::*)(unsigned) const'. 
     return (num < 2) ? num : F::get(this)(num - 1) + F::get(this)(num - 2); 
    } 
    unsigned long calculateAndStore (unsigned num) { 
     using F = MemberFunctionMemoizer<FibonacciCalculator, unsigned long (FibonacciCalculator::*)(unsigned), &FibonacciCalculator::calculateAndStore>; // 'decltype(&FibonacciCalculator::calculateAndStore)' can be used in place of 'unsigned long (FibonacciCalculator::*)(unsigned)'. 
     const unsigned long result = (num < 2) ? num : F::get(this)(num - 1) + F::get(this)(num - 2); 
     computed.push_back(result); 
     return result; 
    } 
}; 

int main() { 
    FibonacciCalculator fib; 
    for (unsigned i = 1; i < 20; i++) 
     std::cout << fib.calculate(i) << ' '; // 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 
    std::cout << '\n'; 

    for (unsigned i = 1; i < 20; i++) 
     std::cout << fib.calculateAndStore(i) << ' '; // 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 
    std::cout << '\n'; 
    for (unsigned long x : fib.computed) 
     std::cout << x << ' '; // 1 1 0 1 1 2 2 3 3 5 5 8 8 13 13 21 21 34 34 55 55 89 89 144 144 233 233 377 377 610 610 987 987 1597 1597 2584 2584 4181 
    std::cout << '\n'; 
}