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,我有一个类模板 template< typename G, int N > class Foo { /* ... */ }; 我想让任何类型的GFoo成为类Bar的朋友。正确的语法是什么 谢谢大家! 在C++03中,这是不可能的;C++标准的145.3/9表示如下: 友元声明不应声明部分专门化 正如在另一个答案中所指出的,这个问题可能有一些解决方法,但是您所要求的特定功能在该标准中不可用 幸运的是,C++11现在得到了很好的支持,通过指定模板别名的功能,我们可以实现以下目标: te

我有一个类模板

template< typename G, int N > class Foo { /* ... */ };
我想让任何类型的G
Foo
成为
类Bar
的朋友。正确的语法是什么


谢谢大家!

在C++03中,这是不可能的;C++标准的145.3/9表示如下:

友元声明不应声明部分专门化

正如在另一个答案中所指出的,这个问题可能有一些解决方法,但是您所要求的特定功能在该标准中不可用

幸运的是,C++11现在得到了很好的支持,通过指定模板别名的功能,我们可以实现以下目标:

template <typename, typename> struct X{};

template <typename T> 
struct Y
{
    template <typename U> using X_partial = X<T, U>;
    template <typename> friend class X_partial;
};
模板结构X{};
模板
结构
{
使用X_partial=X的模板;
模板朋友类X_部分;
};

如果没有C++11,我认为最好的方法是使用一个伪类型别名,这可能需要一些代码(构造函数)复制(这可能无法解决您尝试的实际问题):

templateclass Foo{/*…*/};
模板类fooalis:public Foo{};
模板
分类栏{
模板好友类fooalis;
/* ... */
};

注意:
模板好友类fooalis=>此处的
G
是不必要的。它使用gcc编译,但无法使用clang版本3.8编译。错误消息是
error:X_partial重新定义为不同类型的符号模板friend class X_partial。您在clang上看到的错误应该与无法在friend语句中使用typedefs有关:
template <typename, typename> struct X{};

template <typename T> 
struct Y
{
    template <typename U> using X_partial = X<T, U>;
    template <typename> friend class X_partial;
};
template< typename G, int N > class Foo { /* ... */ };

template<typename G> class FooAlias : public Foo<G, 0> { };

template< typename T >
class Bar {
  template< typename G > friend class FooAlias;

  /* ... */
};