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

TICK_MEMBER_REQUIRES_OC for outside-class-definitions of members #11

Open
wants to merge 2 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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,25 @@ struct foo
};
```

If the member is defined outside of the class, use `TICK_MEMBER_REQUIRES_OC` (OC: outside_class) outside the class. For example,
```cpp
template<class T>
struct foo
{
T x;

TICK_MEMBER_REQUIRES(is_incrementable<T>())
void up();
};

template<class T>
TICK_MEMBER_REQUIRES_OC(is_incrementable<T>())
void foo<T>::up()
{
x++;
}
```

TICK_PARAM_REQUIRES
--------------------

Expand Down
19 changes: 19 additions & 0 deletions doc/src/requires.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ struct foo
};
```

If the member is defined outside of the class, use `TICK_MEMBER_REQUIRES_OC` (OC: outside_class) outside the class. For example,
```cpp
template<class T>
struct foo
{
T x;

TICK_MEMBER_REQUIRES(is_incrementable<T>())
void up();
};

template<class T>
TICK_MEMBER_REQUIRES_OC(is_incrementable<T>())
void foo<T>::up()
{
x++;
}
```

TICK_PARAM_REQUIRES
--------------------

Expand Down
34 changes: 34 additions & 0 deletions test/requires.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <type_traits>
#include <tick/integral_constant.h>
#include <tick/requires.h>
#include <cassert>

struct not_int {};

Expand Down Expand Up @@ -163,3 +164,36 @@ TICK_STATIC_TEST_CASE()
STATIC_ASSERT_SAME(decltype(check_param_trait_requires(not_int())), std::false_type);
};



template<class T>
struct member_requires_oc
{
T x;

TICK_MEMBER_REQUIRES(std::is_same<T, int>::value)
int foo();

TICK_MEMBER_REQUIRES(std::is_same<T, char>::value)
char foo();
};

template<class T>
TICK_MEMBER_REQUIRES_OC(std::is_same<T, int>::value)
int member_requires_oc<T>::foo()
{
return 123;
}

template<class T>
TICK_MEMBER_REQUIRES_OC(std::is_same<T, char>::value)
char member_requires_oc<T>::foo()
{
return 'a';
}

TICK_TEST_CASE()
{
assert(member_requires_oc<int>().foo() == 123);
assert(member_requires_oc<char>().foo() == 'a');
}
8 changes: 5 additions & 3 deletions tick/requires.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ trait(Ts&&...) noexcept

#define TICK_CLASS_REQUIRES(...) typename std::enable_if<(__VA_ARGS__)>::type

#define TICK_REQUIRES(...) bool TickPrivateBool ## __LINE__=true, typename std::enable_if<(TickPrivateBool##__LINE__ && __VA_ARGS__), int>::type = 0
#define TICK_REQUIRES(...) bool TickPrivateBool ## __LINE__=true, typename std::enable_if<(TickPrivateBool##__LINE__ && __VA_ARGS__), int>::type = 0
#define TICK_REQUIRES_OC(...) bool TickPrivateBool ## __LINE__, typename std::enable_if<(TickPrivateBool##__LINE__ && __VA_ARGS__), int>::type

#define TICK_MEMBER_REQUIRES(...) template<TICK_REQUIRES(__VA_ARGS__)>
#define TICK_MEMBER_REQUIRES(...) template<TICK_REQUIRES( __VA_ARGS__)>
#define TICK_MEMBER_REQUIRES_OC(...) template<TICK_REQUIRES_OC(__VA_ARGS__)>

#define TICK_PARAM_REQUIRES(...) \
typename std::enable_if< \
(tick::detail::param_extract<decltype(__VA_ARGS__)>::value), \
typename tick::detail::private_enum<__LINE__>::type \
>::type = tick::detail::private_enum<__LINE__>::type::na

#endif
#endif