Problem with vector<bool>

I have an tree containing a vector. Unfortunately I cannot access the elements of the vector. In the CINT interpretted mode ROOT will not access the variable. In compiled code (with g++) I get segmentation violation when the element is accessed.

This seems to be a general problem inside ROOT:

for example:

#include <vector>
vector<bool>    testvector;
testvector.push_back(1);
testvector.push_back(1);
testvector.push_back(0);

std::cout << testvector.size() << std::endl;
std::cout << testvector.at(0) << std::endl;

size() is returned correctly but accessing an element produces the following error:

Error: Can't call vector<bool,allocator<bool> >::at(0) in current scope (tmpfile):1: Possible candidates are... (in vector<bool,allocator<bool> >) *** Interpreter error recovered ***

Is there a solution to this?

(I am running ROOT version 5.18 on Scientific Linux 4)

Hi,

Indeed it seems that the dictionary for vector of bool is incomplete.

To work around the problem, compile (via ACLiC) the script(s) that need to use vector

Cheers,
Philippe

I can’t comment on exactly why this is being rejected (although one of the many headers for vector in the 5.20 distribution actually ifdef’s out the failing operators in interpreted mode … I don’t have a problem with your example in compiled mode). But if you have the option, it is strongly suggest that you don’t use vector. Apologies if you already know this, but vector is not really a vector, and probably doesn’t work the way you think it does. It’s a Standard C++ template container specialization that fails to meet the requirements of a Standard C++ container. It’s standardization is widely regarded to have been a major blunder by the members of the committee that standardized it, but it can’t be removed without breaking backward compatibility.

For some discussion of this topic, any Google search on vector will pull up hundreds of links discussing the problems with the class. For instance: informit.com/guides/content. … &seqNum=98

The usual suggestion is to use std::vector, std::deque or std::bitset (the first two if you need a dynamically sized container, the latter if you can get by with a staticly sized container). I don’t know whether Root supports those out of the box in interpreted code, however.

[quote]The usual suggestion is to use std::vector, std::deque or std::bitset (the first two if you need a dynamically sized container, the latter if you can get by with a staticly sized container). I don’t know whether Root supports those out of the box in interpreted code, however.[/quote]Those are indeed very good suggestion. Note that ROOT I/O does not support std::bitset (yet?)

Cheers,
Philippe.

Hi,

Thanks for your help. Unfortunately I have no way to avoid the use of the nefarious vector as I am analysing ntuples that have been produced by others.

My current solution is to download a copy of the ntuples to my institute, strip out the vector (using a compiled ACliC script) and replace them with vector. Not the most elegant solution but it should work!

The problem seems to be that ACliC knows how to get out the vector from the Tree but g++ doesn’t know. Perhaps it is impossible, but is there a way to get g++ to do as ACliC does?

By ‘g++’ I suppose you actually meant ‘using my own executable’ instead of root.exe. In this case all you need to do is to add the following command to your main:gROOT->ProcessLine("#include <vector>");

Cheers,
Philippe.

Yes that’s exactly what I meant.

And that worked like a charm! Thank you!