Some chapter 34 notes.
array
There’s a fixed size array type designed to replace raw C style arrays. It doesn’t appear that it is bounds checked by default, and the Xcode7 (clang) compiler doesn’t do bounds checking for it right now. Here’s an example
#include <array> using a10 = std::array<int, 10> ; void foo( a10 & a ) { a[3] = 7 ; a[13] = 7 ; } void bar( int * a ) { a[3] = 7 ; a[13] = 7 ; }
The generated asm for both of these is identical
$ gobjdump -d --reloc -C --no-show-raw-insn d.o d.o: file format mach-o-x86-64 Disassembly of section .text: 0000000000000000 <foo(std::__1::array<int, 10ul>&)>: 0: push %rbp 1: mov %rsp,%rbp 4: movl $0x7,0xc(%rdi) b: movl $0x7,0x34(%rdi) 12: pop %rbp 13: retq 14: data16 data16 nopw %cs:0x0(%rax,%rax,1) 0000000000000020 <bar(int*)>: 20: push %rbp 21: mov %rsp,%rbp 24: movl $0x7,0xc(%rdi) 2b: movl $0x7,0x34(%rdi) 32: pop %rbp 33: retq 34: data16 data16 nopw %cs:0x0(%rax,%rax,1)
The foo() function here is also not compile-time bounds checked if the out of bounds access is changed to
however, this does at least generate an out of bounds error
$ ./d libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: array::at Abort trap: 6
Even though we don’t get compile-time bounds checking (at least with the current clang compiler), array has the nice advantage of knowing its own size, so you can’t screw it up:
bitset and vector bool
The bitset class provides a fixed size bit array that appears to be formed from an array of register sized words. On a 64-bit platform (mac+xcode 7) I’m seeing that sizeof() == 8 for <= 64 bits, and doubles after that for <= 128 bits.
The code for something like the following (set two bits), is pretty decent, basically a single or immediate instruction:
Array access operators are provided to access each bit position:
for ( int i{} ; i < v.size() ; i++ ) { char sep{ ' ' } ; if ( ((i+1) % 8) == 0 ) { sep = '\n' ; } std::cout << v[i] << sep ; } std::cout << '\n' ;
There is no range-for support built in for this class. I was able to implement a wrapper that allowed that using a wrapper class
template <int N> struct iter ; template <int N> struct mybits : public std::bitset<N> { using T = std::bitset<N> ; using T::T ; using T::size ; inline iter<N> begin( ) ; inline iter<N> end( ) ; } ;
and a helper iterator
template <int N> struct iter { unsigned pos{} ; const mybits<N> & b ; iter( const mybits<N> & bits, unsigned p = {} ) : pos{p}, b{bits} {} const iter & operator++() { pos++ ; return *this ; } bool operator != ( const iter & i ) const { return pos != i.pos ; } int operator*() const { return b[ pos ] ; } } ;
plus the begin and end function bodies required for the loop
template <int N> inline iter<N> mybits<N>::begin( ) { return iter<N>( *this ) ; } template <int N> inline iter<N> mybits<N>::end( ) { return iter<N>( *this, size() ) ; }
I’m not sure what the rationale for not including such range for support is, when std::vector has exactly that? vector is a vector specialization that is also supposed to be compact, but unlike bitset, allows for a variable sized bit array.
bitset also has a number of handy type conversion operators that vector does not (to string, and string to integer)
tuple
The std::tuple type generalizes std::pair, allowing for easy structures of N different types.
I saw that tuple has a tie method that allows it to behave very much like a perl array assignment. Such an assignment looks like
#!/usr/bin/perl my ($a, $b, $c) = foo() ; printf( "%0.1f $b $c\n", $a ) ; exit 0 ; sub foo { return (1.0, "blah", 3) ; }
A similar C++ equivalent is more verbose
#include <tuple> #include <stdio.h> using T = std::tuple<float, const char *, int> ; T foo() { return std::make_tuple( 1.0, "blah", 3 ) ; } int main() { float f ; const char * k ; int i ; std::tie( f, k, i ) = foo() ; printf("%f %s %d\n", f, k, i ) ; return 0 ; }
I was curious how the code that accepts a tuple return using tie, using different variables (as above), and using a structure return differed
In each case, using -O2 and the Xcode 7 compiler (clang), a printf function similar to the above ends up looking pretty much uniformly like:
$ gobjdump -d --reloc -C --no-show-raw-insn u.o ... 0000000000000110 <h()>: 110: push %rbp 111: mov %rsp,%rbp 114: sub $0x20,%rsp 118: lea -0x18(%rbp),%rdi 11c: callq 121 <h()+0x11> 11d: BRANCH32 foo() 121: mov -0x10(%rbp),%rsi 125: mov -0x8(%rbp),%edx 128: movss -0x18(%rbp),%xmm0 12d: cvtss2sd %xmm0,%xmm0 131: lea 0xd(%rip),%rdi # 145 <h()+0x35> 134: DISP32 .cstring-0x145 138: mov $0x1,%al 13a: callq 13f <h()+0x2f> 13b: BRANCH32 printf 13f: add $0x20,%rsp 143: pop %rbp 144: retq
The generated code is pretty much dominated by the stack pushing required for the printf call. I used printf here instead of std::cout because the generated code for std::cout is so crappy looking (and verbose).
shared_ptr
Reading the section on shared_ptr, it wasn’t obvious that it was a thread safe interface. I wondered if some sort of specialization was required to make the reference counting thread safe. It appears that thread safety is built in
This can also be seen in the debugger (assuming the gcc libstdc++ is representitive)
Breakpoint 1, main () at sharedptr.cc:33 33 std::shared_ptr<T> p = std::make_shared<T>() ; Missing separate debuginfos, use: debuginfo-install libgcc-4.8.5-4.el7.x86_64 libstdc++-4.8.5-4.el7.x86_64 (gdb) n 35 foo( p ) ; (gdb) s std::shared_ptr<T>::shared_ptr (this=0x7fffffffe060) at /usr/include/c++/4.8.2/bits/shared_ptr.h:103 103 shared_ptr(const shared_ptr&) noexcept = default; (gdb) s std::__shared_ptr<T, (__gnu_cxx::_Lock_policy)2>::__shared_ptr (this=0x7fffffffe060) at /usr/include/c++/4.8.2/bits/shared_ptr_base.h:779 779 __shared_ptr(const __shared_ptr&) noexcept = default; (gdb) s std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count (this=0x7fffffffe068, __r=...) at /usr/include/c++/4.8.2/bits/shared_ptr_base.h:550 550 : _M_pi(__r._M_pi) (gdb) s 552 if (_M_pi != 0) (gdb) s 553 _M_pi->_M_add_ref_copy(); (gdb) s std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_add_ref_copy (this=0x607010) at /usr/include/c++/4.8.2/bits/shared_ptr_base.h:131 131 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
This was looking at a call of the following form