[Trilinos-Users] problem with Teuchos arrays of type bool

Kevin Long kevin.long at ttu.edu
Sat Jun 6 16:15:55 MDT 2009


Kurtis,

Array<T> uses std::vector<T> as its data representation. Unfortunately, 
std::vector<bool> defers to a specialization which does not implement the 
entire std::vector interface.  In particular, the STL specialization of 
vector<bool> does not allow random access to individual elements and so is 
incompatible with its use in Teuchos::Array. Random access is used in 
Array<T>::toString(), and hence you code fails to compile. 

This is why you see the problem only with bool. 

The simple fix is to use, say, Array<char> in place of Array<bool>.

This problem could also be fixed by rewriting Array<T>::toString() in terms of 
sequential iterators. However, vector<bool> is best avoided in any case 
because its use is error-prone.

Kevin

 
On Saturday 06 June 2009 04:24:50 pm Kurtis Nusbaum wrote:
> I'm having trouble compiling some code using a Teuchos::Array<bool>. Here's
> the problem.
>
> Code for main.cpp:
> .....
> 12  Teuchos::Array<bool> testBoolArray;
> 13  testBoolArray.push_back(true);
> 14  testBoolArray.push_back(false);
> 15  testBoolArray.push_back(true);
> 16  testBoolArray.push_back(false);
> 17  std::string test = testBoolArray.toString();
> ....
>
> This code generates the following compile time error:
>
> g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB
> -DQT_CORE_LIB -DQT_SHARED -I../../qt4.5/mkspecs/linux-g++-64 -I.
> -I../../qt4.5/include/QtCore -I../../qt4.5/include/QtGui
> -I../../qt4.5/include -I. -I. -I. -I.
> -I/net/home/f07/klnusbau/trilinos9/stratdebugBuild/include -o main.o
> main.cpp
> /net/home/f07/klnusbau/trilinos9/stratdebugBuild/include/Teuchos_ArrayRCP.h
>pp: In function ‘Teuchos::ArrayRCP<const T> Teuchos::arcp(const
> Teuchos::RCP<const std::vector<T, std::allocator<_CharT> > >&) [with T =
> bool]’:
> /net/home/f07/klnusbau/trilinos9/stratdebugBuild/include/Teuchos_Array.hpp:
>761:   instantiated from ‘typename Teuchos::Array<T>::const_iterator
> Teuchos::Array<T>::begin() const [with T = bool]’
> /net/home/f07/klnusbau/trilinos9/stratdebugBuild/include/Teuchos_Array.hpp:
>1191:   instantiated from ‘Teuchos::ArrayView<const T>
> Teuchos::Array<T>::view(Teuchos_Index, Teuchos_Index) const [with T =
> bool]’
> /net/home/f07/klnusbau/trilinos9/stratdebugBuild/include/Teuchos_Array.hpp:
>1231:   instantiated from ‘Teuchos::ArrayView<const T>
> Teuchos::Array<T>::operator()() const [with T = bool]’
> /net/home/f07/klnusbau/trilinos9/stratdebugBuild/include/Teuchos_Array.hpp:
>1109:   instantiated from ‘std::string Teuchos::Array<T>::toString() const
> [with T = bool]’ main.cpp:17:   instantiated from here
> /net/home/f07/klnusbau/trilinos9/stratdebugBuild/include/Teuchos_ArrayRCP.h
>pp:814: error: lvalue required as unary ‘&’ operand
>
> I am at an absolute loss. If I do the exact same thing but just use a
> different data type like int instead of bool everything works just fine.
> Any ideas?
>
> -Kurtis



-- 
------------------------------------------------------
Kevin Long
Associate Professor
Department of Mathematics and Statistics
Texas Tech University
Lubbock, TX 

"The end of fear is the beginning of wisdom" 
    -- Bertrand Russell
------------------------------------------------------




More information about the Trilinos-Users mailing list