【发布时间】:2026-02-09 08:25:01
【问题描述】:
考虑以下代码:
template<class T, class F> struct X {};
template<class T, class F, T F::* m> struct Y {};
struct Foo {
int member;
typedef X<int, Foo> x_type; // works well
typedef Y<int, Foo, &Foo::member> y_type; // ERROR
};
typedef Y<int, Foo, &Foo::member> y_type2; // OK
为什么编译器会产生错误? (VS2008)
新
我已将此错误发布到connect.microsoft.com。
【问题讨论】:
-
你用的是什么编译器? g++ 4.3.3 没有错误。
-
我无法理解“Y”模板的声明。模板参数是类型或整数值。 "TF::* m" 不是类型,你可以用它来参数化你的模板...当你写 "Y
" 时,最后一个参数不是类型也不是整数价值。它是指向成员的指针。所以,我相信,它必须被拒绝...... -
在 Visual Studio 2008 中:"'Foo::member' : 不是类型名称、静态或枚举数" 并且在同一行:"'member' :未声明的标识符”
-
从 14.1 开始:非类型模板参数应具有以下类型之一 [...] 指向成员的指针。
-
@roe,不,我们不能随心所欲地改变规则。指针和成员指针就是它们的样子,整数/整数就是它们的样子。成员指针甚至不是 C++ 中的指针。它们是 member 指针,是一个单独的类型类别。
标签: c++ templates pointer-to-member