gpt4 book ai didi

c++ - 定义类模板的友元函数模板

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:01 26 4
gpt4 key购买 nike

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

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){
^

最佳答案

编辑后,一种可能的方法(可能不会比您的解决方法更好 - 但适用于所有调用)是为参数 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>的 friend 当你想让它成为任何 test<...> 的 friend 时.您应该执行以下操作:

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);
}

这里你是说:

Any function test<K> foo<U, K> (test<U> const) is a friend of any test<...>.

关于c++ - 定义类模板的友元函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37882049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com