相关疑难解决方法(0)

C++,函数指针到模板函数指针

我有一个指向常见静态方法的指针

class MyClass
{
  private:
    static double ( *pfunction ) ( const Object *, const Object *);
    ...
};
Run Code Online (Sandbox Code Playgroud)

指向静态方法

 class SomeClass
 {
  public:
    static double getA ( const Object *o1, const Object *o2);
    ...
 };
Run Code Online (Sandbox Code Playgroud)

初始化:

double ( *MyClass::pfunction ) ( const Object *o1, const Object *o2 )  = &SomeClass::getA;
Run Code Online (Sandbox Code Playgroud)

我想将此指针转换为静态模板函数指针:

template <class T>
static T ( *pfunction ) ( const Object <T> *, const Object <T> *); //Compile error
Run Code Online (Sandbox Code Playgroud)

哪里:

 class SomeClass
 {
  public:
    template <class T>
    static …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-pointers

21
推荐指数
3
解决办法
5万
查看次数

部分专业化消歧优先链的更好模式?

考虑以下系列的部分专业化:

template <typename T, typename Enable=void>
struct foo {
  void operator()() const { cout << "unspecialized" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  is_integral<T>::value
>>{
  void operator()() const { cout << "is_integral" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  sizeof(T) == 4
    and not is_integral<T>::value
>>{
  void operator()() const { cout << "size 4" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  is_fundamental<T>::value
    and not (sizeof(T) == 4)
    and not is_integral<T>::value
>>{ …
Run Code Online (Sandbox Code Playgroud)

c++ templates idioms partial-specialization

5
推荐指数
1
解决办法
227
查看次数