![]() |
template won't compile when used with bool
this template won't compile when used with bool, the erorr is
"invalid initialization of non const reference" template < typename T > class Vec : public std::vector< T > { public: Vec() { } Vec( int s ) : std::vector<T>(s) { } T& operator[](int i) { return this -> at(i); } // <- this line const T& operator[](int i) const { return this -> at(i); } }; Vec<bool> b; |
Re: template won't compile when used with bool
Greg wrote:
> this template won't compile when used with bool, the erorr is > "invalid initialization of non const reference" > > template < typename T > > class Vec : public std::vector< T > { > public: > Vec() { } > Vec( int s ) : std::vector<T>(s) { } > T& operator[](int i) { return this -> at(i); } > // <- this line > const T& operator[](int i) const { return this -> at(i); } > }; > > Vec<bool> b; Don't inherit from a std classes that are not meant to be inherited from. Prefer composition when you can (cf. http://www.parashift.com/c++-faq-lit...html#faq-24.3). Anyway, it's likely breaking because the standard library provides a specialization for std::vector<bool>. Cheers! --M |
Re: template won't compile when used with bool
Greg wrote:
> this template won't compile when used with bool, the erorr is > "invalid initialization of non const reference" > > template < typename T > > class Vec : public std::vector< T > { > public: > Vec() { } > Vec( int s ) : std::vector<T>(s) { } > T& operator[](int i) { return this -> at(i); } > // <- this line > const T& operator[](int i) const { return this -> at(i); } > }; > > Vec<bool> b; > std::vector<bool> is specialized and the operator[] and at() functions do not return bool&. |
Re: template won't compile when used with bool
I only started using this template for debugging purposes, I don't need
it for the time being. |
| All times are GMT. The time now is 10:53 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.