Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 建立依赖模板类的友谊_C++_Templates_Friend - Fatal编程技术网

C++ 建立依赖模板类的友谊

C++ 建立依赖模板类的友谊,c++,templates,friend,C++,Templates,Friend,在我的代码中有一个例子,我需要将依赖模板类作为朋友,但我认为我已经用尽了所有的可能性,并且没有一个可行。这有可能吗?如果是,怎么做 最简单的例子: struct Traits { template <typename T> struct Foo { typedef typename T::bar* type; }; }; template <typename T> class Bar { typedef int bar;

在我的代码中有一个例子,我需要将依赖模板类作为朋友,但我认为我已经用尽了所有的可能性,并且没有一个可行。这有可能吗?如果是,怎么做

最简单的例子:

struct Traits {
    template <typename T>
    struct Foo {
        typedef typename T::bar* type;
    };
};

template <typename T>
class Bar
{
    typedef int bar;
    // need to make T::Foo<Bar> a friend somehow?
    typedef typename T::template Foo<Bar>::type type; // won't compile because bar is private... 
                                                      // suppose I cannot make bar public.


    type val;
};

int main() {
    Bar<Traits> b;
}
struct特性{
模板
结构Foo{
typedef typename T::bar*类型;
};
};
模板
分类栏
{
输入条;
//需要让T::Foo成为朋友吗?
typedef typename T::template Foo::type;//无法编译,因为bar是私有的。。。
//假设我不能公开酒吧。
val型;
};
int main(){
b栏;
}
试试下面的方法

using my_friend=typename T::template Foo<Bar>;
friend my_friend;
使用my_friend=typename T::template Foo;
朋友,我的朋友;

在您的类()中,我能够通过在您的注释处插入以下内容来编译它:

friend struct Traits::Foo<Bar>;
friend-struct-Traits::Foo;
结构特征{
模板
结构Foo{
typedef typename T::bar*类型;
};
};
模板
分类栏
{
输入条;
朋友类型名T::模板Foo;
typedef typename T::template Foo::type type;
val型;
};
int main(){
b栏;
}

老实说,这样的事情对你不是更好吗

template <typename T>
class Bar;

struct Traits
{
public:
  template <typename T>
  struct Foo
  {
    typedef Bar<T> bar_type;
    typedef typename bar_type::bar* type;
  };
};

template <typename T>
class Bar
{
  typedef int bar;
  typedef Bar self_type;
  typedef struct Traits::template Foo<T> traits_type;

  friend traits_type;

  typedef typename traits_type::type type;

  type val;
};


int main(int argc,char** argv)
{
  Bar<Traits> b;
}
模板
分类栏;
结构特征
{
公众:
模板
结构Foo
{
类型def Bar_类型;
typedef typename bar_type::bar*type;
};
};
模板
分类栏
{
输入条;
typedef-Bar-self_-type;
typedef struct Traits::模板Foo Traits\u type;
友人特质(u型),;
typedef typename traits_type::type type type;
val型;
};
int main(int argc,字符**argv)
{
b栏;
}

这比允许任何包含公共typedef类型的东西成为你的朋友要安全得多,尽管这一切……都很奇怪。

他想把这些特征作为模板参数,这毫无意义。如果你想为任何一个只愿意将自己用作模板参数的类提供友谊,只需将一切
公开
。任何想抢你隐私的人都可以成为你的朋友。为什么要麻烦?但是您可以说(在C++11中)
friendtypename T::templatefoo如果你想的话。或者直接
朋友typename T::template Foo工作too@texasbruce是的,但有时我更喜欢typedef,以使事情更可读(至少对我来说)。模板可能会变得非常复杂,并且与朋友(或模板朋友)结合使用,这是我经常看不到的东西,因此我更喜欢更详细的内容。适用于gcc 4.7.3,而不是4.6.4(“声明朋友时必须使用类密钥”)。我猜这只是旧gcc中的一个bug?@Barry,更像是尚未实现,而不是bug,但是是的,我在4.8.1上测试了它,效果很好。
friend typename
是C++11功能,在C++03中不可用。
template <typename T>
class Bar;

struct Traits
{
public:
  template <typename T>
  struct Foo
  {
    typedef Bar<T> bar_type;
    typedef typename bar_type::bar* type;
  };
};

template <typename T>
class Bar
{
  typedef int bar;
  typedef Bar self_type;
  typedef struct Traits::template Foo<T> traits_type;

  friend traits_type;

  typedef typename traits_type::type type;

  type val;
};


int main(int argc,char** argv)
{
  Bar<Traits> b;
}