相关疑难解决方法(0)

我必须在何处以及为何要使用"模板"和"typename"关键字?

在模板,在那里,为什么我必须把typenametemplate上依赖的名字呢?究竟什么是依赖名称?我有以下代码:

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)

c++ templates c++-faq dependent-name typename

1061
推荐指数
8
解决办法
15万
查看次数

内联朋友功能的范围是什么?

在搜索到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)

c++ scope inline friend

32
推荐指数
2
解决办法
5911
查看次数

标签 统计

c++ ×2

c++-faq ×1

dependent-name ×1

friend ×1

inline ×1

scope ×1

templates ×1

typename ×1