Skip to content

Instantly share code, notes, and snippets.

@BruJu
Last active October 31, 2020 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruJu/23460924540a125a1e51ff7e7c65b201 to your computer and use it in GitHub Desktop.
Save BruJu/23460924540a125a1e51ff7e7c65b201 to your computer and use it in GitHub Desktop.
MonStructCommaAdd.cpp
#include <iostream>
#include <string>
// Trying to understand / implement myself the magic behind
// https://www.boost.org/doc/libs/1_74_0/libs/assign/doc/index.html#operator+=
template<typename ... Ts>
void Print(const char * format, Ts ... ts) {
char buffer[256];
std::sprintf(buffer, format, ts ...);
std::cout << buffer << '\n';
}
struct MonStruct {
int x = 0;
MonStruct (int pX) : x(pX) {}
friend MonStruct & operator+=(MonStruct & lhs, int rhs) {
Print("MonStruct(%d) += int(%d)", lhs.x, rhs);
lhs.x += rhs;
return lhs;
}
friend MonStruct & operator,(MonStruct & lhs, int rhs) {
Print("MonStruct(%d), int(%d)", lhs.x, rhs);
lhs.x += rhs;
return lhs;
}
};
int main() {
MonStruct monStruct(1);
monStruct += 10, 100, 1000, 10000;
Print("R = MonStruct(%d)", monStruct.x);
// MonStruct(1) += int(10)
// MonStruct(11), int(100)
// MonStruct(111), int(1000)
// MonStruct(1111), int(10000)
// R = MonStruct(11111)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment