Created
April 4, 2014 06:55
-
-
Save nkoneko/9969460 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> | |
template <int N> | |
struct Integer | |
{ | |
int& ref() | |
{ | |
return x_; | |
} | |
Integer& add(int const x) | |
{ | |
x_ += x; | |
return *this; | |
} | |
Integer& operator+=(int const& x) | |
{ | |
x_ += x; | |
return *this; | |
} | |
int x_ = N; | |
}; | |
template <int N> | |
::std::ostream& operator<<(::std::ostream& s, Integer<N> const& i) | |
{ | |
s << i.x_; | |
return s; | |
} | |
int main() | |
{ | |
Integer<1> one; | |
std::cout << one // => 1 | |
<< std::endl; | |
Integer<2> two; | |
std::cout << (two.add(6) += 4) // => 12 | |
<< std::endl; | |
Integer<3> three; | |
three.ref() = 5; | |
std::cout << three // => 5 | |
<< std::endl; | |
Integer<4> four; | |
std::cout << ((four += 4).add(16) += 4) | |
// => 28 | |
<< std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment