Skip to content

Instantly share code, notes, and snippets.

@nkoneko
Created April 4, 2014 06:55
Show Gist options
  • Save nkoneko/9969460 to your computer and use it in GitHub Desktop.
Save nkoneko/9969460 to your computer and use it in GitHub Desktop.
#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