Skip to content

Commit

Permalink
Merge pull request #194 from arximboldi/fix-cpp20
Browse files Browse the repository at this point in the history
Fix C++20 issues
  • Loading branch information
arximboldi authored Nov 8, 2021
2 parents 0d20729 + da6bd91 commit 372f99f
Show file tree
Hide file tree
Showing 15 changed files with 16,001 additions and 7,333 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ jobs:
toolchain: llvm-9
std: 17
opts: ['fuzzers']
# std 20
- type: Debug
toolchain: gnu-11
std: 20
- type: Debug
toolchain: llvm-13
std: 20
# sanitizers
- type: Debug
toolchain: llvm-8
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
project(immer VERSION 0.6.2)

if (NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wno-unused-parameter -Wno-extended-offsetof -Wno-c++17-extensions -Wno-c++1z-extensions -Wno-unknown-warning-option")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wno-unused-parameter -Wno-extended-offsetof -Wno-c++17-extensions -Wno-c++1z-extensions -Wno-unknown-warning-option -Wno-type-limits")
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
set(CMAKE_CXX_EXTENSIONS off)
Expand Down
80 changes: 62 additions & 18 deletions immer/box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class box
{}

public:
const holder* impl() const { return impl_; };

using value_type = T;
using memory_policy = MemoryPolicy;

Expand Down Expand Up @@ -131,24 +133,6 @@ class box
/*! Access via pointer member access */
const T* operator->() const { return &get(); }

/*! Comparison. */
IMMER_NODISCARD bool operator==(detail::exact_t<const box&> other) const
{
return impl_ == other.value.impl_ || get() == other.value.get();
}
// Note that the `exact_t` disambiguates comparisons against `T{}`
// directly. In that case we want to use `operator T&` and
// compare directly. We definitely never want to convert a value
// to a box (which causes an allocation) just to compare it.
IMMER_NODISCARD bool operator!=(detail::exact_t<const box&> other) const
{
return !(*this == other.value);
}
IMMER_NODISCARD bool operator<(detail::exact_t<const box&> other) const
{
return get() < other.value.get();
}

/*!
* Returns a new box built by applying the `fn` to the underlying
* value.
Expand Down Expand Up @@ -180,6 +164,66 @@ class box
}
};

template <typename T, typename MP>
IMMER_NODISCARD bool operator==(const box<T, MP>& a, const box<T, MP>& b)
{
return a.impl() == b.impl() || a.get() == b.get();
}
template <typename T, typename MP>
IMMER_NODISCARD bool operator!=(const box<T, MP>& a, const box<T, MP>& b)
{
return a.impl() != b.impl() && a.get() != b.get();
}
template <typename T, typename MP>
IMMER_NODISCARD bool operator<(const box<T, MP>& a, const box<T, MP>& b)
{
return a.impl() != b.impl() && a.get() < b.get();
}

template <typename T, typename MP, typename T2>
IMMER_NODISCARD auto operator==(const box<T, MP>& a, T2&& b)
-> std::enable_if_t<!std::is_same<box<T, MP>, std::decay_t<T2>>::value,
decltype(a.get() == b)>
{
return a.get() == b;
}
template <typename T, typename MP, typename T2>
IMMER_NODISCARD auto operator!=(const box<T, MP>& a, T2&& b)
-> std::enable_if_t<!std::is_same<box<T, MP>, std::decay_t<T2>>::value,
decltype(a.get() != b)>
{
return a.get() != b;
}
template <typename T, typename MP, typename T2>
IMMER_NODISCARD auto operator<(const box<T, MP>& a, T2&& b)
-> std::enable_if_t<!std::is_same<box<T, MP>, std::decay_t<T2>>::value,
decltype(a.get() < b)>
{
return a.get() < b;
}

template <typename T2, typename T, typename MP>
IMMER_NODISCARD auto operator==(T2&& b, const box<T, MP>& a)
-> std::enable_if_t<!std::is_same<box<T, MP>, std::decay_t<T2>>::value,
decltype(a.get() == b)>
{
return a.get() == b;
}
template <typename T2, typename T, typename MP>
IMMER_NODISCARD auto operator!=(T2&& b, const box<T, MP>& a)
-> std::enable_if_t<!std::is_same<box<T, MP>, std::decay_t<T2>>::value,
decltype(a.get() != b)>
{
return a.get() != b;
}
template <typename T2, typename T, typename MP>
IMMER_NODISCARD auto operator<(T2&& b, const box<T, MP>& a)
-> std::enable_if_t<!std::is_same<box<T, MP>, std::decay_t<T2>>::value,
decltype(a.get() < b)>
{
return a.get() < b;
}

} // namespace immer

namespace std {
Expand Down
28 changes: 14 additions & 14 deletions immer/detail/iterator_facade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ class iterator_facade
return derived() + n;
}

bool operator==(const DerivedT& rhs) const
friend bool operator==(const DerivedT& a, const DerivedT& b)
{
return access_t::equal(derived(), rhs);
return access_t::equal(a, b);
}
bool operator!=(const DerivedT& rhs) const
friend bool operator!=(const DerivedT& a, const DerivedT& b)
{
return !access_t::equal(derived(), rhs);
return !access_t::equal(a, b);
}

DerivedT& operator++()
Expand Down Expand Up @@ -180,31 +180,31 @@ class iterator_facade
auto tmp = derived();
return tmp -= n;
}
DifferenceTypeT operator-(const DerivedT& rhs) const
friend DifferenceTypeT operator-(const DerivedT& a, const DerivedT& b)
{
static_assert(is_random_access, "");
return access_t::distance_to(rhs, derived());
return access_t::distance_to(b, a);
}

bool operator<(const DerivedT& rhs) const
friend bool operator<(const DerivedT& a, const DerivedT& b)
{
static_assert(is_random_access, "");
return access_t::distance_to(derived(), rhs) > 0;
return access_t::distance_to(a, b) > 0;
}
bool operator<=(const DerivedT& rhs) const
friend bool operator<=(const DerivedT& a, const DerivedT& b)
{
static_assert(is_random_access, "");
return access_t::distance_to(derived(), rhs) >= 0;
return access_t::distance_to(a, b) >= 0;
}
bool operator>(const DerivedT& rhs) const
friend bool operator>(const DerivedT& a, const DerivedT& b)
{
static_assert(is_random_access, "");
return access_t::distance_to(derived(), rhs) < 0;
return access_t::distance_to(a, b) < 0;
}
bool operator>=(const DerivedT& rhs) const
friend bool operator>=(const DerivedT& a, const DerivedT& b)
{
static_assert(is_random_access, "");
return access_t::distance_to(derived(), rhs) <= 0;
return access_t::distance_to(a, b) <= 0;
}
};

Expand Down
29 changes: 15 additions & 14 deletions immer/detail/rbts/rrbtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#include <immer/detail/type_traits.hpp>

#include <cassert>
#include <stdexcept>
#include <limits>
#include <memory>
#include <numeric>
#include <limits>
#include <stdexcept>

namespace immer {
namespace detail {
Expand Down Expand Up @@ -158,9 +158,10 @@ struct rrbtree
{
auto r = root->relaxed();
assert(r == nullptr || r->d.count);
return r ? r->d.sizes[r->d.count - 1]
: size ? (size - 1) & ~mask<BL>
/* otherwise */ : 0;
return r ? r->d.sizes[r->d.count - 1]
: size ? (size - 1) & ~mask<BL>
/* otherwise */
: 0;
}

template <typename Visitor, typename... Args>
Expand Down Expand Up @@ -310,15 +311,15 @@ struct rrbtree
return tail_off == tail_off_other
? make_leaf_sub_pos(tail, tail_size())
.visit(equals_visitor{}, other.tail)
: tail_off > tail_off_other
? std::equal(tail->leaf(),
tail->leaf() + (size - tail_off),
other.tail->leaf() +
(tail_off - tail_off_other))
/* otherwise */
: std::equal(tail->leaf(),
tail->leaf() + (size - tail_off),
iter_t{other} + tail_off);
: tail_off > tail_off_other
? std::equal(tail->leaf(),
tail->leaf() + (size - tail_off),
other.tail->leaf() +
(tail_off - tail_off_other))
/* otherwise */
: std::equal(tail->leaf(),
tail->leaf() + (size - tail_off),
iter_t{other} + tail_off);
}

std::tuple<shift_t, node_t*> push_tail(node_t* root,
Expand Down
3 changes: 3 additions & 0 deletions immer/transience/gc_transience_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ struct gc_transience_policy
struct edit
{
void* v;
edit(void* v_)
: v{v_}
{}
edit() = delete;
bool operator==(edit x) const { return v == x.v; }
bool operator!=(edit x) const { return v != x.v; }
Expand Down
10 changes: 5 additions & 5 deletions nix/benchmarks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rec {
};
nativeBuildInputs = [ autoreconfHook ];
propagatedBuildInputs = [ boehmgc ];
meta = with stdenv.lib; {
meta = with lib; {
homepage = "http://hypirion.com/thesis";
description = "RRB-tree implemented as a library in C. ";
license = licenses.mit;
Expand All @@ -34,7 +34,7 @@ rec {
};
dontBuild = true;
installPhase = "mkdir -vp $out/include; cp -vr $src/steady $out/include/";
meta = with stdenv.lib; {
meta = with lib; {
homepage = "https://github.com/marcusz/steady";
description = "This is a fast and reliable persistent (immutable) vector class for C++";
license = licenses.asl20;
Expand All @@ -53,7 +53,7 @@ rec {
};
dontBuild = true;
installPhase = "mkdir -vp $out/include/chunkedseq; cp -vr $src/include/* $out/include/chunkedseq/";
meta = with stdenv.lib; {
meta = with lib; {
homepage = "http://deepsea.inria.fr/chunkedseq";
description = "Container data structure for representing sequences by many fixed-capacity heap-allocated buffers--chunks";
license = licenses.mit;
Expand All @@ -72,7 +72,7 @@ rec {
};
dontBuild = true;
installPhase = "mkdir -vp $out/include; cp -vr $src/immutable $out/include/";
meta = with stdenv.lib; {
meta = with lib; {
homepage = "https://github.com/rsms/immutable-cpp";
description = "Persistent immutable data structures for C++";
license = licenses.mit;
Expand All @@ -91,7 +91,7 @@ rec {
};
dontBuild = true;
installPhase = "mkdir -vp $out/include; cp -vr $src/hash_trie.hpp $out/include/";
meta = with stdenv.lib; {
meta = with lib; {
homepage = "https://github.com/rsms/immutable-cpp";
description = "Persistent immutable data structures for C++";
license = licenses.mit;
Expand Down
19 changes: 13 additions & 6 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{ toolchain ? "",
nixpkgs ? (import <nixpkgs> {}).fetchFromGitHub {
owner = "NixOS";
repo = "nixpkgs";
rev = "053ad4e0db7241ae6a02394d62750fdc5d64aa9f";
sha256 = "11l9sr8zg8j1n5p43zjkqwpj59gn8c84z1kf16icnsbnv2smzqdc";
}}:
rev ? "08ef0f28e3a41424b92ba1d203de64257a9fca6a",
sha256 ? "1mql1gp86bk6pfsrp0lcww6hw5civi6f8542d4nh356506jdxmcy",
nixpkgs ? builtins.fetchTarball {
name = "nixpkgs-${rev}";
url = "https://github.com/nixos/nixpkgs/archive/${rev}.tar.gz";
sha256 = sha256;
},
}:

with import nixpkgs {};

Expand All @@ -26,6 +28,8 @@ let
if toolchain == "gnu-7" then { stdenv = gcc7Stdenv; cc = gcc7; } else
if toolchain == "gnu-8" then { stdenv = gcc8Stdenv; cc = gcc8; } else
if toolchain == "gnu-9" then { stdenv = gcc9Stdenv; cc = gcc9; } else
if toolchain == "gnu-10" then { stdenv = gcc10Stdenv; cc = gcc10; } else
if toolchain == "gnu-11" then { stdenv = gcc11Stdenv; cc = gcc11; } else
if toolchain == "llvm-39" then { stdenv = llvmPackages_39.libcxxStdenv; cc = llvmPackages_39.libcxxClang; } else
if toolchain == "llvm-4" then { stdenv = llvmPackages_4.libcxxStdenv; cc = llvmPackages_4.libcxxClang; } else
if toolchain == "llvm-5" then { stdenv = llvmPackages_5.libcxxStdenv; cc = llvmPackages_5.libcxxClang; } else
Expand All @@ -34,6 +38,9 @@ let
if toolchain == "llvm-8" then { stdenv = llvmPackages_8.libcxxStdenv; cc = llvmPackages_8.libcxxClang; } else
if toolchain == "llvm-9" then { stdenv = llvmPackages_9.stdenv; cc = llvmPackages_9.clang; } else
if toolchain == "llvm-10" then { stdenv = llvmPackages_10.stdenv; cc = llvmPackages_10.clang; } else
if toolchain == "llvm-11" then { stdenv = llvmPackages_11.stdenv; cc = llvmPackages_11.clang; } else
if toolchain == "llvm-12" then { stdenv = llvmPackages_12.stdenv; cc = llvmPackages_12.clang; } else
if toolchain == "llvm-13" then { stdenv = llvmPackages_13.stdenv; cc = llvmPackages_13.clang; } else
abort "unknown toolchain";

in
Expand Down
Loading

0 comments on commit 372f99f

Please sign in to comment.