Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:13:55+00:00 2026-06-17T11:13:55+00:00

I have a templated class that has a data member of type std::vector<T> ,

  • 0

I have a templated class that has a data member of type std::vector<T>, where T is also a parameter of my templated class.

In my template class I have quite some logic that does this:

T &value = m_vector[index];

This doesn’t seem to compile when T is a boolean, because the [] operator of std::vector does not return a bool-reference, but a different type.

Some alternatives (although I don’t like any of them):

  • tell my users that they must not use bool as template parameter
  • have a specialization of my class for bool (but this requires some code duplication)

Isn’t there a way to tell std::vector not to specialize for bool?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:13:56+00:00Added an answer on June 17, 2026 at 11:13 am

    You simply cannot have templated code behave regularly for T equal to bool if your data is represented by std::vector<bool> because this is not a container. As pointed out by @Mark Ransom, you could use std::vector<char> instead, e.g. through a trait like this

    template<typename T> struct vector_trait { typedef std::vector<T> type; };
    template<> struct vector_trait<bool> { typedef std::vector<char> type; };
    

    and then use typename vector_trait<T>::type wherever you currently use std::vector<T>. The disadvantage here is that you need to use casts to convert from char to bool.

    An alternative as suggested in your own answer is to write a wrapper with implicit conversion and constructor

    template<typename T>
    class wrapper
    {
    public:
            wrapper() : value_(T()) {}
            /* explicit */ wrapper(T const& t): value_(t) {}
            /* explicit */ operator T() { return value_; }
    private:
            T value_;
    };
    

    and use std::vector< wrapper<bool> > everywhere without ever having to cast. However, there are also disadvantages to this because standard conversion sequences containing real bool parameters behave differently than the user-defined conversions with wrapper<bool> (the compiler can at most use 1 user-defined conversion, and as many standard conversions as necessary). This means that template code with function overloading can subtly break. You could uncomment the explicit keywords in the code above but that introduces the verbosity again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class template that has a boost matrix as a private member
I have a templated container class that looks something like this: template <class ItemType>
I have a custom iterator template class that wraps up a std::list iterator. In
I have a template class with a variadic template member function that I am
I have a template function that is enabled (through a std::enable_if) its parameter is
I have a class C, which has a string* ps private data member. Now,
I have a templated class that I want to avoid copying (because of the
I want to have a template class that looks something like what I have
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have the following use case: There's a class called Template and with that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.