这个c++模板化函数在做什么?
what is this c++ templated function doing
我想写一个薄薄的包装围绕AngelScript。我在弄清楚如何环绕一个特定的结构体时遇到了麻烦。
下面是我要包装的结构体asSMethodPtr
的结构体定义:
template <int N>
struct asSMethodPtr
{
template<class M>
static asSFuncPtr Convert(M Mthd)
{
// This version of the function should never be executed, nor compiled,
// as it would mean that the size of the method pointer cannot be determined.
int ERROR_UnsupportedMethodPtr[N-100];
asSFuncPtr p;
return p;
}
};
以下是asSFuncPtr
的定义:
struct asSFuncPtr
{
union
{
char dummy[25]; // largest known class method pointer
struct {asFUNCTION_t func; char dummy[25-sizeof(asFUNCTION_t)];} f;
} ptr;
asBYTE flag; // 1 = generic, 2 = global func
};
这是我找到的代码(取自AngelBinder库),允许我"包装"它:
template<typename R> ClassExporter& method(std::string name, R (T::*func)())
{
MethodClass mthd(name, Type<R>::toString(), asSMethodPtr< sizeof( void (T::*)() ) >::Convert( AS_METHOD_AMBIGUITY_CAST( R (T::*)()) (func) ));
this->_methods.push(mthd);
return *this;
}
不幸的是,我不知道这段代码在做什么…
T::*
应该做什么?指向类类型的指针?
R (T::*func)()
是什么?
T::*
是指向成员的指针。R (T::*func)()
是指向成员函数的指针,该成员函数返回R
并接受0个形参。例如:
struct S
{
int f()
{
return 5;
}
int x = 10;
};
int main()
{
S s;
int S::* ptr = &S::x;
std::cout << s.*ptr; // 10
int (S::*f_ptr)() = &S::f;
std::cout << (s.*f_ptr)(); // 5
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 什么时候调用析构函数
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 是什么让放置新调用对象的构造函数?
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 在C 中,对2D数组的增量是什么?函数断言(0)做什么?
- 我应该使用什么函数签名来返回对可能不存在的对象的引用
- 一个类的构造函数,为另一个类进行强制转换.将调用什么函数
- 在 opencv for c 中,什么函数与 Mat::convertTo 和 cvtColor() 完全相同
- 当按下Alt-Enter键时调用什么函数
- C库导出什么函数?
- 使用什么函数来选择文本
- c++在空类中编写和调用什么函数?
- 在共享库的构造函数(_init部分)中,如何知道什么函数被中断了?
- 我可以用什么函数来获取按钮的把手