Last active
August 29, 2015 14:10
-
-
Save upsuper/322bde839f3552ff7e3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
template<class T> | |
class Out | |
{ | |
private: | |
T& mValue; | |
public: | |
explicit Out(T& aValue) | |
: mValue(aValue) | |
{} | |
operator T&() const | |
{ | |
return mValue; | |
} | |
template<class U> | |
Out<T>& operator=(const U& aValue) | |
{ | |
mValue = aValue; | |
return *this; | |
} | |
}; | |
template<class T> | |
class Out<T*> | |
{ | |
private: | |
T* mValue; | |
public: | |
explicit Out(T* aValue) | |
: mValue(aValue) | |
{} | |
operator T*() const | |
{ | |
return mValue; | |
} | |
}; | |
template<class T> | |
Out<T> make_out(T& aValue) | |
{ | |
return Out<T>(aValue); | |
} | |
template<class T> | |
Out<T*> make_out(T* aValue) | |
{ | |
return Out<T*>(aValue); | |
} | |
void funcA(int* arg) | |
{ | |
*arg = 10; | |
} | |
void funcB(Out<int*> arg) | |
{ | |
*arg = 20; | |
} | |
void funcC(int& arg) | |
{ | |
arg = 30; | |
} | |
void funcD(Out<int>& arg) | |
{ | |
arg = 40; | |
} | |
int main() | |
{ | |
int x; | |
funcA(make_out(&x)); | |
std::cout << x << std::endl; | |
funcB(make_out(&x)); | |
std::cout << x << std::endl; | |
funcC(make_out(x)); | |
std::cout << x << std::endl; | |
funcD(make_out(x)); | |
std::cout << x << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment