Skip to content

Instantly share code, notes, and snippets.

@be9
Created March 10, 2014 03:51
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 be9/9459195 to your computer and use it in GitHub Desktop.
Save be9/9459195 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
using namespace std;
template <typename T>
class Initializer {
public:
Initializer(T &_array)
: array(_array) {}
Initializer<T> operator,(int x) {
array.push_back(x);
return Initializer<T>(array);
}
private:
Initializer();
protected:
T &array;
};
template <typename T>
class InitSwitch {
public:
InitSwitch(T &_array, int _value)
: array(_array), value(_value), wipe_on_destruct(true) {}
InitSwitch(const InitSwitch<T> &other)
: array(other.array), value(other.value), wipe_on_destruct(true) {
other.wipe_on_destruct = false;
}
~InitSwitch() {
if (wipe_on_destruct)
array.push_back(value);
}
Initializer<T> operator,(int x) {
wipe_on_destruct = false;
array.push_back(value);
array.push_back(x);
return Initializer<T>(array);
}
private:
InitSwitch();
protected:
T &array;
int value;
mutable bool wipe_on_destruct;
};
class CoolVector : public vector<int> {
public:
InitSwitch<CoolVector> operator=(int x) {
return InitSwitch<CoolVector>(*this, x);
}
};
int main() {
CoolVector vec;
vec = 1,2,3;
for (int i = 0; i < vec.size(); ++i)
cout << vec[i] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment