如何传递模板模板非类型成员函数指针?
How to pass a template template non-type member function pointer?
我想将指向另一个模板成员函数的指针作为模板模板非类型参数传递(给模板成员函数)。
这是我试过的方法:
enum Unit { model_unit, nanometers, meters };
struct Material {
double rho;
};
struct Point {
double x, y, z;
};
struct Impl{
template<Unit unit>
Material * LookupMat_1(const Point& p) {
return nullptr; // let's suppose it returns a valid pointer
}
template<Unit unit>
Material * LookupMat_2(const Point& p) {
return nullptr; // let's suppose it returns a valid pointer
}
// compiler error here:
// expected 'class' or 'typename' before 'Material'
// template<template<Unit> Material * (Impl::*LookupFunc)(const Point&)
// ^~~~~~~~
template<template<Unit> Material * (Impl::*LookupFunc)(const Point&) >
Material * GetMaterial(const Point & p) {
return (this->*LookupFunc<Unit::model_unit>)(p);
}
void DoSomething() {
Point p = {};
auto mat_1 = GetMaterial<LookupMat_1>(p);
auto mat_2 = GetMaterial<LookupMat_2>(p);
}
};
int main() {
Impl i;
i.DoSomething();
}
我的语法错误,编译器说:
main.cpp:25:29: error: expected 'class' or 'typename' before 'Material'
template<template<Unit> Material * (Impl::*LookupFunc)(const Point&)
^~~~~~~~
我想不出正确的语法。
LookupFunc
是类型 Material * (Impl::*)(const Point&)
的模板,它是指向成员函数的指针。
我想做的事情可行吗?
我错过了什么?
正如评论中所解释的,没有一个指向模板函数(或方法)的指针,因为它不是一个函数而是一组函数。
我能想象的最好的做类似的事情(我的意思是......解释 GetMaterial()
中的 Unit
类型)是在 [=16] 中添加几个 sub-structs =] 使用静态模板方法
struct lm1
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
struct lm2
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
然后重写GetMaterial()
如下
template <typename T>
Material * GetMaterial (Point const & p)
{ return T::template func<Unit::model_unit>(p); }
并这样使用
void DoSomething()
{
Point p = {};
auto mat_1 = GetMaterial<lm1>(p);
auto mat_2 = GetMaterial<lm2>(p);
}
通过这种方式,您可以将包含全套模板函数的单一类型(lm1
或 lm2
)传递给 GetMaterial()
;然后,在 GetMaterial()
中,你 select 正确的函数解释 Unit::model_unit
.
以下是一个完整的工作示例
enum Unit { model_unit, nanometers, meters };
struct Material
{ double rho; };
struct Point
{ double x, y, z; };
struct Impl
{
struct lm1
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
struct lm2
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
template <typename T>
Material * GetMaterial (Point const & p)
{ return T::template func<Unit::model_unit>(p); }
void DoSomething()
{
Point p = {};
auto mat_1 = GetMaterial<lm1>(p);
auto mat_2 = GetMaterial<lm2>(p);
}
};
int main ()
{
Impl i;
i.DoSomething();
}
这是 class-template-as-functor 的方法。
template<Unit unit>
struct LookupMat_1
{
Material * operator()(const Point& p) {
return nullptr;
}
};
template<Unit unit>
struct LookupMat_2
{
Material * operator()(const Point& p) {
return nullptr;
}
};
template<template<Unit> typename LookupMat>
Material * GetMaterial(const Point & p)
{
return LookupMat<Unit::model_unit>()(p);
}
我想将指向另一个模板成员函数的指针作为模板模板非类型参数传递(给模板成员函数)。
这是我试过的方法:
enum Unit { model_unit, nanometers, meters };
struct Material {
double rho;
};
struct Point {
double x, y, z;
};
struct Impl{
template<Unit unit>
Material * LookupMat_1(const Point& p) {
return nullptr; // let's suppose it returns a valid pointer
}
template<Unit unit>
Material * LookupMat_2(const Point& p) {
return nullptr; // let's suppose it returns a valid pointer
}
// compiler error here:
// expected 'class' or 'typename' before 'Material'
// template<template<Unit> Material * (Impl::*LookupFunc)(const Point&)
// ^~~~~~~~
template<template<Unit> Material * (Impl::*LookupFunc)(const Point&) >
Material * GetMaterial(const Point & p) {
return (this->*LookupFunc<Unit::model_unit>)(p);
}
void DoSomething() {
Point p = {};
auto mat_1 = GetMaterial<LookupMat_1>(p);
auto mat_2 = GetMaterial<LookupMat_2>(p);
}
};
int main() {
Impl i;
i.DoSomething();
}
我的语法错误,编译器说:
main.cpp:25:29: error: expected 'class' or 'typename' before 'Material' template<template<Unit> Material * (Impl::*LookupFunc)(const Point&) ^~~~~~~~
我想不出正确的语法。
LookupFunc
是类型 Material * (Impl::*)(const Point&)
的模板,它是指向成员函数的指针。
我想做的事情可行吗?
我错过了什么?
正如评论中所解释的,没有一个指向模板函数(或方法)的指针,因为它不是一个函数而是一组函数。
我能想象的最好的做类似的事情(我的意思是......解释 GetMaterial()
中的 Unit
类型)是在 [=16] 中添加几个 sub-structs =] 使用静态模板方法
struct lm1
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
struct lm2
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
然后重写GetMaterial()
如下
template <typename T>
Material * GetMaterial (Point const & p)
{ return T::template func<Unit::model_unit>(p); }
并这样使用
void DoSomething()
{
Point p = {};
auto mat_1 = GetMaterial<lm1>(p);
auto mat_2 = GetMaterial<lm2>(p);
}
通过这种方式,您可以将包含全套模板函数的单一类型(lm1
或 lm2
)传递给 GetMaterial()
;然后,在 GetMaterial()
中,你 select 正确的函数解释 Unit::model_unit
.
以下是一个完整的工作示例
enum Unit { model_unit, nanometers, meters };
struct Material
{ double rho; };
struct Point
{ double x, y, z; };
struct Impl
{
struct lm1
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
struct lm2
{
template <Unit U>
static Material * func (Point const & p)
{ return nullptr; }
};
template <typename T>
Material * GetMaterial (Point const & p)
{ return T::template func<Unit::model_unit>(p); }
void DoSomething()
{
Point p = {};
auto mat_1 = GetMaterial<lm1>(p);
auto mat_2 = GetMaterial<lm2>(p);
}
};
int main ()
{
Impl i;
i.DoSomething();
}
这是 class-template-as-functor 的方法。
template<Unit unit>
struct LookupMat_1
{
Material * operator()(const Point& p) {
return nullptr;
}
};
template<Unit unit>
struct LookupMat_2
{
Material * operator()(const Point& p) {
return nullptr;
}
};
template<template<Unit> typename LookupMat>
Material * GetMaterial(const Point & p)
{
return LookupMat<Unit::model_unit>()(p);
}