Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template won't compile when used with bool

Reply
Thread Tools

template won't compile when used with bool

 
 
Greg
Guest
Posts: n/a
 
      10-19-2006
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;

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-19-2006
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

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-19-2006
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&.
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-19-2006
I only started using this template for debugging purposes, I don't need
it for the time being.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template template par used for a container statistic er C++ 1 09-05-2007 07:07 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Explicit template instantiation from template function doesn't compile? Fernando Cuenca C++ 4 09-06-2004 04:06 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments