1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | namespace Rose { template<typename T> struct RemoveReference { typedef T Type; }; template<typename T> struct RemoveReference<T &> { typedef T Type; }; template<typename... Elems> class Tuple; template<typename First, typename... Elems> class Tuple<First, Elems...> { public: Tuple(First a, Elems... more) : More(more...), Element(a) { } Tuple<First, Elems...> & operator=(const Tuple<RemoveReference<First>::Type, RemoveReference<Elems>::Type...> & rhs) { this->Element = rhs.Element; this->More = rhs.More; return *this; } private: Tuple<Elems...> More; First Element; }; template<typename Only> class Tuple<Only> { public: Tuple(Only a) : Element(a) { } Tuple<Only> & operator=(const Tuple<RemoveReference<Only>::Type> & rhs) { this->Element = rhs.Element; return *this; } private: Only Element; }; } int main() { Rose::Tuple<int, float, int> t(1, 1.f, 2); } |
bmFtZXNwYWNlIFJvc2UKewogICAgdGVtcGxhdGU8dHlwZW5hbWUgVD4KICAgIHN0cnVjdCBSZW1vdmVSZWZlcmVuY2UKICAgIHsKICAgICAgICB0eXBlZGVmIFQgVHlwZTsKICAgIH07CgogICAgdGVtcGxhdGU8dHlwZW5hbWUgVD4KICAgIHN0cnVjdCBSZW1vdmVSZWZlcmVuY2U8VCAmPgogICAgewogICAgICAgIHR5cGVkZWYgVCBUeXBlOwogICAgfTsKCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZS4uLiBFbGVtcz4KICAgIGNsYXNzIFR1cGxlOwogICAgCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZSBGaXJzdCwgdHlwZW5hbWUuLi4gRWxlbXM+CiAgICBjbGFzcyBUdXBsZTxGaXJzdCwgRWxlbXMuLi4+CiAgICB7CiAgICBwdWJsaWM6CiAgICAgICAgVHVwbGUoRmlyc3QgYSwgRWxlbXMuLi4gbW9yZSkKICAgICAgICAgICAgOiBNb3JlKG1vcmUuLi4pLCBFbGVtZW50KGEpCiAgICAgICAgewogICAgICAgIH0KICAgICAgICAKICAgICAgICBUdXBsZTxGaXJzdCwgRWxlbXMuLi4+ICYgb3BlcmF0b3I9KGNvbnN0IFR1cGxlPFJlbW92ZVJlZmVyZW5jZTxGaXJzdD46OlR5cGUsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBSZW1vdmVSZWZlcmVuY2U8RWxlbXM+OjpUeXBlLi4uPiAmIHJocykKICAgICAgICB7CiAgICAgICAgICAgIHRoaXMtPkVsZW1lbnQgPSByaHMuRWxlbWVudDsKICAgICAgICAgICAgdGhpcy0+TW9yZSA9IHJocy5Nb3JlOwoKICAgICAgICAgICAgcmV0dXJuICp0aGlzOwogICAgICAgIH0KCiAgICBwcml2YXRlOgogICAgICAgIFR1cGxlPEVsZW1zLi4uPiBNb3JlOwogICAgICAgIEZpcnN0IEVsZW1lbnQ7CiAgICB9OwogICAgCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZSBPbmx5PgogICAgY2xhc3MgVHVwbGU8T25seT4KICAgIHsKICAgIHB1YmxpYzoKICAgICAgICBUdXBsZShPbmx5IGEpIDogRWxlbWVudChhKQogICAgICAgIHsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgVHVwbGU8T25seT4gJiBvcGVyYXRvcj0oY29uc3QgVHVwbGU8UmVtb3ZlUmVmZXJlbmNlPE9ubHk+OjpUeXBlPiAmIHJocykKICAgICAgICB7CiAgICAgICAgICAgIHRoaXMtPkVsZW1lbnQgPSByaHMuRWxlbWVudDsKCiAgICAgICAgICAgIHJldHVybiAqdGhpczsKICAgICAgICB9CgogICAgcHJpdmF0ZToKICAgICAgICBPbmx5IEVsZW1lbnQ7CiAgICB9Owp9CgppbnQgbWFpbigpCnsKICAgIFJvc2U6OlR1cGxlPGludCwgZmxvYXQsIGludD4gdCgxLCAxLmYsIDIpOwp9
prog.cpp:28:75: error: type/value mismatch at argument 1 in template parameter list for 'template<class ... Elems> struct Rose::Tuple' prog.cpp:28:75: error: expected a type, got 'Rose::RemoveReference<First>::Type' prog.cpp:28:75: error: type/value mismatch at argument 1 in template parameter list for 'template<class ... Elems> struct Rose::Tuple' prog.cpp:28:75: error: expected a type, got 'Rose::RemoveReference<Elems>::Type ...' prog.cpp: In member function 'Rose::Tuple<First, Elems ...>& Rose::Tuple<First, Elems ...>::operator=(const int&)': prog.cpp:30:33: error: request for member 'Element' in 'rhs', which is of non-class type 'const int' prog.cpp:31:30: error: request for member 'More' in 'rhs', which is of non-class type 'const int' prog.cpp: At global scope: prog.cpp:49:72: error: type/value mismatch at argument 1 in template parameter list for 'template<class ... Elems> struct Rose::Tuple' prog.cpp:49:72: error: expected a type, got 'Rose::RemoveReference<First>::Type' prog.cpp: In member function 'Rose::Tuple<Only>& Rose::Tuple<Only>::operator=(const int&)': prog.cpp:51:33: error: request for member 'Element' in 'rhs', which is of non-class type 'const int'
-
result: Compilation error (maybe you wish to see an example for C++11)


