Skip to content

Commit

Permalink
Closes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
evaleev committed Jul 26, 2014
1 parent 261d255 commit 85ad0d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
27 changes: 27 additions & 0 deletions btas/util/sequence_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ namespace std {
// access
operator type& () noexcept { return *this; }
type& get() noexcept { return *this; }
const type& get() const noexcept { return *this; }

};

// re-implement reference_wrapper<const infinite_sequence_adaptor<Ptr>> to act like const Ptr
template <typename _T>
class reference_wrapper<const btas::infinite_sequence_adaptor<_T*>> : private btas::infinite_sequence_adaptor<_T*> {
public:

// types
typedef const btas::infinite_sequence_adaptor<_T*> ctype;
typedef btas::infinite_sequence_adaptor<_T*> nctype;

// construct/copy/destroy
reference_wrapper(ctype& x) noexcept : ctype(x) {}
// DO bind to temps
reference_wrapper(nctype&& x) noexcept : ctype(x) {}
reference_wrapper(const reference_wrapper<ctype>& x) noexcept : ctype(x) {}

// assignment
reference_wrapper& operator=(const reference_wrapper<ctype>& x) noexcept {
static_cast<ctype&>(*this) = static_cast<ctype&>(x);
}

// access
operator ctype& () const noexcept { return *this; }
ctype& get() const noexcept { return *this; }

};

Expand Down
5 changes: 3 additions & 2 deletions test/test.C
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,14 @@ int main()
auto map0 = make_map(ptr0, Range{2, 3, 4}); // writable map
auto map0c = make_map(const_cast<const double*>(ptr0), Range{2, 3, 4}); // const map
auto map0c2 = make_cmap(ptr0, Range{2, 3, 4}); // const map
auto map0c3 = make_cmap(const_cast<const double*>(ptr0), Range{2, 3, 4}); // const map

map0(1, 2, 3) = -1.0;
//map0c(1, 2, 3) = -1.0; // error: read-only map
//map0c2(1, 2, 3) = -1.0; // error: read-only map

std::cout << map0 << std::endl;
for (const auto& i: map0) { std::cout << i << std::endl; }
std::cout << map0c << std::endl;
for (const auto& i: map0c) { std::cout << i << std::endl; }

delete[] ptr0;
}
Expand Down

0 comments on commit 85ad0d8

Please sign in to comment.