Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template won't compile when used with bool (http://www.velocityreviews.com/forums/t457727-template-wont-compile-when-used-with-bool.html)

Greg 10-19-2006 11:56 AM

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;


mlimber 10-19-2006 12:23 PM

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


Ron Natalie 10-19-2006 01:07 PM

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&.

Greg 10-19-2006 01:27 PM

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 05:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.