在下面的代码中,目的是有一个reference_wrapper<int> b这样的,即当a改变时,b也改变然而,相反的不应该被允许,即不a应该在改变时b改变。我尝试了两种方法:第 7 行和第 8 行。第 7 行导致编译器抱怨它无法转换为int,const int而第 8 行编译没有问题,但结果不是我想要的(a更改时b更改)。任何想法?
1. #include <iostream>
2. #include <functional>
3. using namespace std;
4.
5. int main() {
6. int a = 1;
7. //reference_wrapper<const int> b = ref(a);
8. //const reference_wrapper<int> b = ref(a);
9. return 0;
10. }