Last active
October 31, 2020 16:14
-
-
Save BruJu/23460924540a125a1e51ff7e7c65b201 to your computer and use it in GitHub Desktop.
MonStructCommaAdd.cpp
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 <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