在模板参数的方法中添加类型转换时出现clang错误 [英] clang error when adding typecast in method of a template parameter
问题描述
我有一个模板结构,该模板结构将方法的类型和方法的指针作为参数,并将其包装在类似C的函数中:
I have a template struct that takes as parameters the type of a method and the pointer to a method and wraps it in a C-like function:
template <typename T, T> struct proxy;
template <typename T, typename R, typename ...Args, R (T::* mf)(Args...)>
struct proxy<R (T::*)(Args...), mf>
{
static R call(T& obj, Args&&... args)
{
return (obj.*mf)(std::forward<Args>(args)...);
}
};
proxy 结构可在预期的简单场景中工作,例如:
The proxy struct works in simple scenarios as expected, eg:
struct Foo
{
int foo(int x)
{
return x + 1;
}
};
...
Foo f;
proxy<int(Foo::*)(int), &Foo::foo>::call(f, 10);
问题是当我在可能展开为以下内容的宏中使用 proxy 时:
The problem is when I use proxy inside macros that may unroll into:
proxy<decltype((int(Foo::*)(int))(&Foo::foo)), (int(Foo::*)(int))(&Foo::foo)>::call(f, 10);
在 clang 中,错误是:
error: non-type template argument is not a pointer to member constant
proxy<decltype((int(Foo::*)(int))(&Foo::foo)), (int(Foo::*)(int))(&Foo::foo)>::call(f, 10);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
另一方面,GCC 4.8未报告任何错误,并且一切正常。
On the other hand GCC 4.8 is not reporting any error and everything works as expected.
我的问题是:
- 是否有解决clang错误的方法,并且
- 这是我应该报告的clang错误吗?
推荐答案
您的宏会生成不符合标准的代码。特别是,在非类型方法指针模板参数中的不允许。在这种情况下,它也是多余的。
Your macro generates standards non-compliant code. In particular, a cast is not allowed in a in a non-type method pointer template argument. It is also redundant in this case.
因此解决此问题的最简单方法是更改宏:
So the easiest way to fix this is to change the macro:
#define WORKING_LUA_METHOD_FLAGS(name_, methodType_, methodPtr_, flags_) \
ANKI_LUA_FUNCTION_AS_METHOD_FLAGS(name_, \
(&proxy<methodType_, methodPtr_>::func), flags_)
在使用时:
ANKI_LUA_METHOD_FLAGS( "bob", (int(Foo::*)(int))(&Foo::foo), empty_flags )
成为:
WORKING_LUA_METHOD_FLAGS( "bob", int(Foo:::*)(int), &Foo::foo, empty_flags )
,现在按照标准是正确的。注意 bob
和 empty_flags
只是占位符。
and now it is correct according to the standard. Note "bob"
and empty_flags
are just placeholders for whatever really goes there. The name replacement may or may not be required.
这篇关于在模板参数的方法中添加类型转换时出现clang错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!