【问题标题】:Define friend function template of class template定义类模板的友元函数模板
【发布时间】:2016-10-19 07:40:58
【问题描述】:

我想定义一个类模板的函数模板。代码如下所示。

template<int M>
struct test{
private:
    int value;

    template<int N = 2 * M>
    friend auto foo(test const t){
        test<N> r;
        r.value = t.value;
        return r;
    }
};

int main(){
    test<1> t;
    foo(t);// expected to return test<2>
    foo<1>(t);// expected to return test<1>
    foo<3>(t);// expected to return test<3>
}

但它不会编译。与之前的问题相比,下面列出了不同之处。

  1. 函数模板的结果涉及类模板的另一个实例化。似乎必须在外部定义功能模板。但我不确定。
  2. 函数模板使用默认模板参数。因此,如果函数模板是在类之外定义的,则需要一个辅助函数模板。

使用g++ -std=c++1z 编译错误:

a.cpp: In instantiation of 'auto foo(test<M>) [with int N = 2; int M = 1]':
a.cpp:16:10:   required from here
a.cpp:4:9: error: 'int test<2>::value' is private
     int value;
         ^
a.cpp:9:17: error: within this context
         r.value = t.value;
                 ^
a.cpp: In instantiation of 'auto foo(test<M>) [with int N = 3; int M = 1]':
a.cpp:18:13:   required from here
a.cpp:4:9: error: 'int test<3>::value' is private
     int value;
         ^
a.cpp:9:17: error: within this context
         r.value = t.value;
                 ^

一种可能的解决方法,但也不正确。

template<int M>
struct test{
private:
    int value;

    template<int NA, int NR>
    friend test<NR> foo_impl(test<NA> const&);
};

template<int NA, int NR>
test<NR> foo_impl(test<NA> const& t){
    test<NR> r;
    r.value = t.value;
    return r;
}

template<int NR, int NA>
auto foo(test<NA> const& t){
    return foo_impl<NA, NR>;
}

template<int NA>
auto foo(test<NA> const& t){
    return foo_impl<NA, NA * 2>(t);
}

int main(){
    test<1> t;
    foo(t);
    foo<3>(t);
    foo<1>(t);
}

错误:

t.cpp: In function 'int main()':
t.cpp:31:13: error: call of overloaded 'foo(test<1>&)' is ambiguous
     foo<1>(t);
             ^
t.cpp:18:6: note: candidate: auto foo(const test<NR>&) [with int NR = 1; int NA = 1]
 auto foo(test<NA> const& t){
      ^
t.cpp:23:6: note: candidate: auto foo(const test<NA>&) [with int NA = 1]
 auto foo(test<NA> const& t){
      ^

【问题讨论】:

  • 当询问为什么不能编译时,通常最好包含编译器错误。

标签: c++ templates friend-function function-templates class-template


【解决方案1】:

编辑后,一种可能的方法(可能不会比您的解决方法更好 - 但适用于所有呼叫)是使用参数 N 的默认值:

template<int M>
struct test{
private:
    int value;

    template<int K, int U, int P>
    friend test<P> foo(test<U> const t);

};

template <int N = 0, int M, int P = ((N == 0) ? M * 2 : N)>
test<P> foo (test<M> const t) {
  test<P> r;
  r.value = t.value;
  return r;
}

int main(){
    test<1> t;
    test<2> p1 = foo(t);
    test<3> p2 = foo<3>(t);
    test<1> p3 = foo<1>(t);
}

这可能不比你的版本漂亮...


这里的问题是您声明foo&lt;N&gt;test&lt;M&gt; 的朋友,而您希望它成为任何test&lt;...&gt; 的朋友。您应该执行以下操作:

template<int M>
struct test{
private:
    int value;

    template<int U, int K>
    friend test<K> foo(test<U> const t);
};

template <int M, int N = 2 * M>
test<N> foo (test<M> const t) {
    test<N> r;
    r.value = t.value;
    return r;
}

int main(){
    test<1> t;
    foo(t);
}

你说的是:

任何函数test&lt;K&gt; foo&lt;U, K&gt; (test&lt;U&gt; const) 都是任何test&lt;...&gt; 的朋友。

【讨论】:

  • 我在main中添加了另一个调用,结果返回类型的tempalte参数应该是第一个。
  • @cqdjyy01234 查看我的编辑,但我不确定在这种情况下你能做一些漂亮的事情(我的版本并不比你的更漂亮......)。
  • @cqdjyy01234 在您最后一次编辑之后,我的代码适用于所有(您的)测试用例,没有模棱两可的调用,您根本无法调用 foo&lt;0&gt; - 但您可以将默认值替换为任何符合您的要求。
猜你喜欢
  • 2016-06-23
  • 2010-12-19
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 2011-07-15
  • 1970-01-01
相关资源
最近更新 更多