T::* 在模板参数中是什么意思?
What does T::* mean in template's parameters?
继 here 中写的文章之后:
我遇到了这段代码(为清楚起见,进行了缩短和更改):
template <class T> struct hasSerialize
{
// This helper struct permits us to check that serialize is truly a method.
// The second argument must be of the type of the first.
// For instance reallyHas<int, 10> would be substituted by reallyHas<int, int 10> and works!
// reallyHas<int, &C::serialize> would be substituted by reallyHas<int, int &C::serialize> and fail!
// Note: It only works with integral constants and pointers (so function pointers work).
// In our case we check that &C::serialize has the same signature as the first argument!
// reallyHas<std::string (C::*)(), &C::serialize> should be substituted by
// reallyHas<std::string (C::*)(), std::string (C::*)() &C::serialize> and work!
template <typename U, U u> struct reallyHas;
// We accept a pointer to our helper struct, in order to avoid to instantiate a real instance of this type.
// std::string (C::*)() is function pointer declaration.
template <typename C>
static char&
test(reallyHas<std::string (C::*)(), &C::serialize>* /*unused*/) { }
};
所以这个
std::string (C::*)()
引起了我的注意。
任何人都可以向我解释 C::*
部分吗?那是一个函数指针 returns std::string
但还有什么?
指向class C 中成员的成员函数指针 returns a std::string
.
检查 isocpp.org 以获取有关指向成员函数的指针的更多信息。
继 here 中写的文章之后:
我遇到了这段代码(为清楚起见,进行了缩短和更改):
template <class T> struct hasSerialize
{
// This helper struct permits us to check that serialize is truly a method.
// The second argument must be of the type of the first.
// For instance reallyHas<int, 10> would be substituted by reallyHas<int, int 10> and works!
// reallyHas<int, &C::serialize> would be substituted by reallyHas<int, int &C::serialize> and fail!
// Note: It only works with integral constants and pointers (so function pointers work).
// In our case we check that &C::serialize has the same signature as the first argument!
// reallyHas<std::string (C::*)(), &C::serialize> should be substituted by
// reallyHas<std::string (C::*)(), std::string (C::*)() &C::serialize> and work!
template <typename U, U u> struct reallyHas;
// We accept a pointer to our helper struct, in order to avoid to instantiate a real instance of this type.
// std::string (C::*)() is function pointer declaration.
template <typename C>
static char&
test(reallyHas<std::string (C::*)(), &C::serialize>* /*unused*/) { }
};
所以这个
std::string (C::*)()
引起了我的注意。
任何人都可以向我解释 C::*
部分吗?那是一个函数指针 returns std::string
但还有什么?
指向class C 中成员的成员函数指针 returns a std::string
.
检查 isocpp.org 以获取有关指向成员函数的指针的更多信息。