Skip to content

Commit

Permalink
Experiments with traits and concepts (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcoe authored Feb 9, 2025
1 parent 758dbdf commit f38761b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions exploration/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")

cc_library(
name = "wrapper",
hdrs = ["wrapper.h"]
Expand All @@ -18,3 +20,14 @@ cc_test(
"@com_google_googletest//:gtest_main",
]
)

cc_library(
name = "traits_and_concepts",
srcs = ["traits_and_concepts.cc"],
visibility = ["//visibility:private"],
)

build_test(
name = "traits_and_concepts_build_test",
targets = ["traits_and_concepts"],
)
12 changes: 12 additions & 0 deletions exploration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,17 @@ if (${XYZ_VALUE_TYPES_IS_NOT_SUBPROJECT})
incomplete_types.cc
incomplete_types_test.cc
)

add_library(traits_and_concepts OBJECT)
target_sources(traits_and_concepts
PRIVATE
traits_and_concepts.cc
)
set_target_properties(traits_and_concepts PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)

endif()
endif()
42 changes: 42 additions & 0 deletions exploration/traits_and_concepts.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// An exploration of traits and concepts in C++ with incomplete types.

#include <concepts>
#include <map>
#include <type_traits>

namespace xyz::testing::traits_and_concepts {

class Incomplete;

// type-trait based checks will fail as type_traits cannot be evaluated on
// incomplete types. AppleClang 16.0.0 (clang-1600.0.26.6):
// error: incomplete type 'xyz::testing::traits_and_concepts::Incomplete' used
// in type trait expression
// static_assert(!std::is_copy_constructible_v<Incomplete>);
// static_assert(std::is_copy_constructible_v<Incomplete>);

// Concept check fails as AppleClang implements concept using type traits.
// AppleClang 16.0.0 (clang-1600.0.26.6):
// error: incomplete type 'xyz::testing::traits_and_concepts::Incomplete' used
// in type trait expression
// static_assert(!std::copy_constructible<Incomplete>);

// Define our own concept to avoid restrictions from type traits.
template <typename T>
concept CopyConstructible = requires(const T& t) {
{ T(t) } -> std::same_as<T>;
};

// The check below passes as the concept's requires clause is not satisfied for
// an incomplete type.
static_assert(!CopyConstructible<Incomplete>);

class Incomplete {};

// The check below will fail if CopyConstructible<Incomplete> was evaluated
// above as the value of a concept does not change from one point of use to
// another. If CopyConstructible<Incomplete> not was evaluated above, the check
// below will pass as Incomplete is now complete and satisfies the concept
// requirements. static_assert(CopyConstructible<Incomplete>);

} // namespace xyz::testing::traits_and_concepts

0 comments on commit f38761b

Please sign in to comment.