template won't compile when used with bool

Discussion in 'C++' started by Greg, Oct 19, 2006.

  1. Greg

    Greg Guest

    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;
     
    Greg, Oct 19, 2006
    #1
    1. Advertising

  2. Greg

    mlimber Guest

    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-lite/private-inheritance.html#faq-24.3).

    Anyway, it's likely breaking because the standard library provides a
    specialization for std::vector<bool>.

    Cheers! --M
     
    mlimber, Oct 19, 2006
    #2
    1. Advertising

  3. Greg

    Ron Natalie Guest

    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&.
     
    Ron Natalie, Oct 19, 2006
    #3
  4. Greg

    Greg Guest

    I only started using this template for debugging purposes, I don't need
    it for the time being.
     
    Greg, Oct 19, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Cuenca
    Replies:
    4
    Views:
    2,539
    Gianni Mariani
    Sep 6, 2004
  2. Nagaraj
    Replies:
    1
    Views:
    893
    Lionel B
    Mar 1, 2007
  3. er
    Replies:
    1
    Views:
    407
    Barry
    Sep 5, 2007
  4. Ethan
    Replies:
    5
    Views:
    396
    Alf P. Steinbach
    Apr 5, 2009
  5. kurt krueckeberg
    Replies:
    1
    Views:
    340
    Victor Bazarov
    Apr 29, 2013
Loading...

Share This Page