Last active
June 29, 2016 21:30
-
-
Save dschwen/488d301568edbb6115fd5fd75d58be3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
template <typename T> | |
class ref_wrapper | |
{ | |
public: | |
ref_wrapper() = delete; | |
ref_wrapper(T & obj) { ptr = &obj; } | |
ref_wrapper(const ref_wrapper<T> & ref) { ptr = ref.ptr; } | |
T & operator =(T value) { *ptr = value; return *ptr; } | |
private: | |
T * ptr; | |
}; | |
std::pair<unsigned int, ref_wrapper<int>> | |
getPair(std::vector<int> & vec, unsigned int index) | |
{ | |
// return std::make_pair(index, ref_wrapper<int>(vec[index])); | |
return std::pair<unsigned int, ref_wrapper<int>>(index, ref_wrapper<int>(vec[index])); | |
} | |
int main() | |
{ | |
std::vector<int> test = { 1, 2, 3, 4 }; | |
for (auto i: test) | |
std::cout << i << ' '; | |
std::cout << '\n'; | |
auto access = getPair(test, 2); | |
access.second = 22; | |
auto wrap = ref_wrapper<int>(test[3]); | |
wrap = 7; | |
for (auto i: test) | |
std::cout << i << ' '; | |
std::cout << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment