【发布时间】:2010-09-24 06:14:58
【问题描述】:
我不明白为什么在下面的代码中,我被允许创建函数print_private_template,而编译器抱怨print_private_class:
#include <cstdio>
class A
{
private:
template <unsigned T>
struct B
{
};
struct C
{
};
public:
template <unsigned T>
B<T> getAb()
{
return B<T>();
}
C getAc()
{
return C();
}
};
template<unsigned T>
void print_private_template(const A::B<T> &ab)
{
printf("%d\n", T);
}
void print_private_class(const A::C &ac)
{
printf("something\n");
}
int main(int, char**)
{
A a;
print_private_template(a.getAb<42>());
print_private_class(a.getAc());
return 0;
}
这是预期的行为吗?编译器错误/扩展?
为了清楚起见,我的目标是让编译器在 both 使用 print_private_template 和 print_private_class 时出错。
【问题讨论】:
-
两者都在MSVC2008中触发编译器错误
标签: c++ templates private-members