Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why for_each cannot choose the correct print here

Tags:

c++

templates

I write a print template function which prints "True" or "False" for bool type value.

I tried to apply it to the for_each algorithm but it does not work.

The output is still 0 or 1. It seems the template specialization does not work in for_each. How can this happen?

How can I make the code work as expected?

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <ctime>

using namespace std;

template <typename T>
void _print(const T& i)
{
    cout<<i<<endl;
}
template <>
void _print<bool>(const bool& i)
{
    if (i == 0)
        cout<<"False"<<endl;
    else
        cout<<"True"<<endl;
}

class print
{
public:
  template <typename T>
  void operator()(const T& val) const { _print(val); };
};

int main()
{
  auto even = [&](int i)->bool{return (i%2==0);};
  srand(time(NULL));
  vector <int> test(3);
  generate(test.begin(),test.end(),[]()->int{return rand()%100;});
  for_each(test.begin(),test.end(),print());
  vector <bool> flag(3);
  transform(test.begin(),test.end(),flag.begin(),even);
  for_each(flag.begin(),flag.end(),print());

  return 0;

}

The output is:

34
23
3
1
0
0

but I expect:

34
23
3
True
False
False
like image 787
iceiceice Avatar asked Dec 08 '22 06:12

iceiceice


1 Answers

The evil part of std::vector<bool>.
vector<bool>::reference is not bool as you may expect but a proxy.

You may add this extra overload (or specialization if you prefer):

void _print(const vector<bool>::reference& i)
{
    _print<bool>(i);
}

Live example

like image 60
Jarod42 Avatar answered Dec 10 '22 20:12

Jarod42