在模板,在那里,为什么我必须把typename和template上依赖的名字呢?究竟什么是依赖名称?我有以下代码:
template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
// ...
template<typename U> struct inUnion {
// Q: where to add typename/template here?
typedef Tail::inUnion<U> dummy;
};
template< > struct inUnion<T> {
};
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
// ...
template<typename U> struct inUnion {
char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …Run Code Online (Sandbox Code Playgroud) 在搜索到SO之后,一个问题告诉我,内联友元函数的词法范围是它定义的类,这意味着它可以访问例如typedef类中的s而不限定它们.但后来我想知道这种功能的实际范围是什么?海湾合作委员会至少拒绝我所有打电话的尝试.可以通过除ADL以外的方式调用示例中的函数,由于没有参数,这是不可能的吗?
标准报价表示赞赏,因为我目前无法访问我的副本.
namespace foo{
struct bar{
friend void baz(){}
void call_friend();
};
}
int main(){
foo::baz(); // can't access through enclosing scope of the class
foo::bar::baz(); // can't access through class scope
}
namespace foo{
void bar::call_friend(){
baz(); // can't access through member function
}
}
Run Code Online (Sandbox Code Playgroud)
导致这些错误:
prog.cpp: In function ‘int main()’:
prog.cpp:9: error: ‘baz’ is not a member of ‘foo’
prog.cpp:10: error: ‘baz’ is not a member of ‘foo::bar’
prog.cpp: In member …Run Code Online (Sandbox Code Playgroud)