Hello. I want to write an initializer list method. I'm looking for the form:
Simple enough, right? I am getting an error whenever I string more than one value on.Code:std::stl_containter<double> thing = initializer<<43.4<<22233.43<<12.32;
And here's the code, only slightly reduced:Code:oop.cpp: In function ‘int main()’: oop.cpp:40: error: no match for ‘operator<<’ in ‘operator<<(dummy_type&, const T&) [with T = int](((const int&)((const int*)(&14)))) << 15’ oop.cpp:32: note: candidates are: init_type<T>& operator<<(init_type<T>&, const T&) [with T = int]
So what's the stupid little thing I'm missing?Code:#include <iostream> #include <vector> using namespace std; template<class T> class init_type { vector<T> v; public: operator vector<T>() { return v; } //and similarly for list, valarray,etc. void push_back(const T & a) { v.push_back(a); } }; class dummy_type {}; dummy_type init; template<class T> init_type<T> operator << (dummy_type & d, const T & t) { init_type<T> ret; ret.push_back(t); return ret; } template<class T> init_type<T> & operator << (init_type<T> & initt, const T & t) { initt.push_back(t); return initt; } int main() { vector<int> v = init<<14<<15; }
Thanks.



LinkBack URL
About LinkBacks


