【发布时间】:2011-02-25 14:25:55
【问题描述】:
template <typename T> class Foo;
template <typename T> int g(Foo<T> const&);
template <typename T> class Foo
{
public:
template <typename U> int f(Foo<U> const& p) const { return p.m; }
// which friend declaration will allow the above function to compile? The
// next one doesn't work.
template <typename U> friend void Foo<U>::template f<T>(Foo<T> const&) const;
// while this one work for g().
friend int g<T>(Foo<T> const&);
private:
int m;
};
template <typename T> int g(Foo<T> const& p) { return p.m; }
// Let's call them
void bar()
{
Foo<int> fi;
Foo<double> fd;
fd.f(fi);
g(fi);
}
上面的代码不能用 g++ 和 Como 编译。 g() 在这里展示我想用 f() 做什么。
例如,这里是 g++ 消息:
foo.cpp:11: error: invalid use of template-id ‘f<T>’ in declaration of primary template
foo.cpp: In member function ‘int Foo<T>::f(const Foo<U>&) const [with U = int, T = double]’:
foo.cpp:27: instantiated from here
foo.cpp:17: error: ‘int Foo<int>::m’ is private
foo.cpp:7: error: within this context
还有科莫的:
"ComeauTest.c", line 11: error: an explicit template argument list is not allowed on
this declaration
template <typename U> friend void Foo<U>::template f<T>(Foo<T> const&) const;
^
"ComeauTest.c", line 7: error: member "Foo<T>::m [with T=int]" (declared at line 17)
is inaccessible
template <typename U> int f(Foo<U> const& p) const { return p.m; }
^
detected during instantiation of "int Foo<T>::f(const Foo<U> &)
const [with T=double, U=int]" at line 27
2 errors detected in the compilation of "ComeauTest.c".
错误消息建议的变体也没有。
顺便说一句,我知道明显的解决方法
template <typename U> friend class Foo<U>;
编辑:
14.5.4/5(n3225的,C++98的14.5.3/6类似,但n3225下面的文字更清楚)开始
类模板的成员可以声明为非模板类的朋友...
这可能意味着类模板的成员可能不会被声明为模板类的朋友,但我的第一个解释是这句话是对以下解释的介绍(主要它们适用于任何专业化,明确与否,给定原型是正确的)。
【问题讨论】:
-
@Johannes Schaub,相似但不同。正如我对 Ise 建议的评论,我想要的是 Foo
的 Foo ::f 朋友,而不是 Foo 的 Foo ::f 朋友。我不会对转换运算符为编译器增加额外的复杂性感到惊讶。