【发布时间】:2014-05-14 00:45:10
【问题描述】:
我不明白为什么 clang 拒绝此代码。我从我的朋友那里得到它,并在 VisualStudio 上为他编译了它......我有很多铿锵声。
#include <utility>
#include <iostream>
template< typename Signature >
class Delegate;
template< typename Ret, typename Param >
class Delegate< Ret(Param) >
{
public:
Ret operator()(Param&& p_param)
{
return m_ifunc(m_obj, std::forward< Param >(p_param));
}
template< typename ObjType, typename Ret(ObjType::*Method)(Param) >
friend auto createDelegate(ObjType * const p_obj)
{
Delegate< Ret(Param) > del;
del.m_obj = p_obj;
del.m_ifunc = &ifunction< ObjType, Method >;
return del;
}
private:
void * const m_obj = nullptr;
Ret (*m_ifunc)(void*, Param&&) = nullptr;
template< typename ObjType, typename Ret(ObjType::*Method)(Param) >
static Ret ifunction(void * const p_obj, Param&& p_param)
{
ObjType * const obj = (ObjType * const) p_obj;
return (obj->*Method)(std::forward< Param >(p_param));
}
};
struct Test
{
void test(int x)
{
std::cout << x << std::endl;
}
};
int main()
{
Test t;
Delegate< void(int) > d = Delegate< void(int) >::createDelegate< Test, &Test::test >(&t);
d(5);
}
这是我得到的错误有人明白发生了什么吗?我已经看到了这种为函数指针指定模板参数的方式,我想这对 clang 的严格性有所遗漏。
main.cpp:17:41: error: expected a qualified name after 'typename'
template< typename ObjType, typename Ret(ObjType::*Method)(Param) >
^
main.cpp:31:41: error: expected a qualified name after 'typename'
template< typename ObjType, typename Ret(ObjType::*Method)(Param) >
^
main.cpp:52:53: error: no member named 'createDelegate' in 'Delegate<void (int)>'
Delegate< void(int) > d = Delegate< void(int) >::createDelegate< Test, &Test::test >(&t);
~~~~~~~~~~~~~~~~~~~~~~~^
main.cpp:52:69: error: 'Test' does not refer to a value
Delegate< void(int) > d = Delegate< void(int) >::createDelegate< Test, &Test::test >(&t);
^
main.cpp:39:8: note: declared here
struct Test
^
main.cpp:52:82: error: definition or redeclaration of 'test' not allowed inside a function
Delegate< void(int) > d = Delegate< void(int) >::createDelegate< Test, &Test::test >(&t);
~~~~~~^
main.cpp:52:86: error: expected ';' at end of declaration
Delegate< void(int) > d = Delegate< void(int) >::createDelegate< Test, &Test::test >(&t);
^
【问题讨论】: