Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Skill to disable default construction. #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions include/NamedType/named_type_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
namespace fluent
{

namespace details {
// The NonDefaultConstructible-Skill has this function as a friend, so we can use it to default-construct it.
template <typename T>
constexpr T construct() noexcept(std::is_nothrow_default_constructible<T>::value) {
return T{};
}
} //namespace details

template <typename T>
using IsNotReference = typename std::enable_if<!std::is_reference<T>::value, void>::type;

Expand All @@ -62,13 +70,14 @@ class FLUENT_EBCO NamedType : public Skills<NamedType<T, Parameter, Skills...>>.
// constructor
NamedType() = default;

explicit constexpr NamedType(T const& value) noexcept(std::is_nothrow_copy_constructible<T>::value) : value_(value)
explicit constexpr NamedType(T const& value) noexcept(std::is_nothrow_copy_constructible<T>::value)
: Skills<NamedType<T, Parameter, Skills...>>{details::construct<Skills<NamedType<T, Parameter, Skills...>>>()}..., value_(value)
{
}

template <typename T_ = T, typename = IsNotReference<T_>>
explicit constexpr NamedType(T&& value) noexcept(std::is_nothrow_move_constructible<T>::value)
: value_(std::move(value))
: Skills<NamedType<T, Parameter, Skills...>>{details::construct<Skills<NamedType<T, Parameter, Skills...>>>()}..., value_(std::move(value))
{
}

Expand Down
11 changes: 11 additions & 0 deletions include/NamedType/underlying_functionalities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ struct MethodCallable<NamedType<T, Parameter, Skills...>> : crtp<NamedType<T, Pa
}
};

template <typename T>
struct NonDefaultConstructible
{
static constexpr auto is_default_constructible = false;

private:
friend NonDefaultConstructible details::construct<NonDefaultConstructible>();

NonDefaultConstructible() = default;
};

template <typename NamedType_>
struct Callable
: FunctionCallable<NamedType_>
Expand Down
10 changes: 10 additions & 0 deletions test/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,3 +1135,13 @@ TEST_CASE("Decrementable")
CHECK( b.get() == 1 );
}
}

TEST_CASE("NonDefaultConstructible")
{
using Strong = fluent::NamedType<int, struct StrongTag, fluent::NonDefaultConstructible>;
static_assert(!std::is_trivially_default_constructible<Strong>::value, "NonDefaultConstructible is default constructible.");
static_assert(std::is_nothrow_constructible<Strong, const int&>::value, "NonDefaultConstructible is not nothrow constructible from value ref.");
static_assert(std::is_nothrow_constructible<Strong, int>::value, "NonDefaultConstructible is not nothrow constructible from value.");
static_assert(std::is_nothrow_copy_constructible<Strong>::value, "NonDefaultConstructible is not nothrow copy constructible.");
static_assert(std::is_nothrow_move_constructible<Strong>::value, "NonDefaultConstructible is not nothrow move constructible.");
}