定义类模板的友元函数模板

4
我想定义一个类模板的函数模板。 代码如下。
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){
      ^

3
询问为什么某个东西无法编译时,通常最好附上编译器错误信息。 - md5i
1个回答

6
在您进行编辑后,一个可能的方法(也许不如您的解决方法好,但适用于所有调用)是使用参数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<N>test<M>的友元,而你希望它成为任何test<...>的友元。你应该按照以下方式进行操作:

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<K> foo<U, K> (test<U> const)都是任何test<...>的友元。


我在主函数中添加了另一个调用,因此返回类型的模板参数应该是第一个。 - cqdjyy01234
@cqdjyy01234 请看我的修改,但我不确定在这种情况下你能够做出更漂亮的东西(我的版本并不比你的更漂亮……)。 - Holt
@cqdjyy01234 在你最后的编辑之后,我的代码在所有(你的)测试用例中都能正常工作,没有任何模棱两可的调用,你只是不能调用 foo<0> - 但你可以将默认值替换为符合你要求的任何内容。 - Holt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接