我尝试使用c ++ 17 auto
非类型模板参数包为类数据成员指针编写代码,但是以下代码clang将编译而gcc不会, you can see godbolt for error message,有人可以告诉我应该相信哪一个,因为我不知道为什么gcc拒绝了。
感谢您的帮助。
template <
typename B,
template <auto B::*...> typename Wrapper,
auto B::*... Args
>
void f(Wrapper<Args...>) {}
template <auto... Args> struct Wrapper {};
struct A { int i; float f; };
// gcc error: unable to deduce 'auto B::*' from '&A::i'
f(Wrapper<&A::i, &A::f>{});
我知道如果将<auto B::*...>
更改为<auto...>
,那么两者都可以编译,但是我想知道为什么,gcc无法推断出正确的类型,因为以下 non -auto 模板参数出现相同的情况:gcc无法编译,clang可以:
template <
typename B,
typename... MT,
template <MT B::*...> class Wrapper,
MT B::*... Args
>
void f(Wrapper<Args...>) {}
答案 0 :(得分:2)
尝试
template <
template <auto ...> typename Wrapper,
auto ... Args
>
void f(Wrapper<Args...>) {}
auto
是B::*
的替代方法;因此您应该同时使用auto
或 B::*
。