Skip to content

Commit e917fe0

Browse files
committed
Add detection trait utility.
1 parent 3c8f2fa commit e917fe0

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

include/nop/traits/is_detected.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 The Native Object Protocols Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef LIBNOP_INCLUDE_NOP_TRAITS_IS_DETECTED_H_
18+
#define LIBNOP_INCLUDE_NOP_TRAITS_IS_DETECTED_H_
19+
20+
#include <type_traits>
21+
22+
#include <nop/traits/void.h>
23+
24+
namespace nop {
25+
26+
namespace detail {
27+
28+
template <template <typename...> class Trait, typename Expression,
29+
typename... Args>
30+
struct IsDetectedType : std::false_type {};
31+
32+
template <template <typename...> class Trait, typename... Args>
33+
struct IsDetectedType<Trait, Void<Trait<Args...>>, Args...> : std::true_type {};
34+
35+
} // namespace detail
36+
37+
// Trait that determines whether the given Trait template is successfully
38+
// evaluated with the given Args. This makes it possible to express SFINAE
39+
// detection expressions as templated aliases instead of full structure
40+
// definitions with partial specializations.
41+
//
42+
// For example, a callable detector may be implemented as follows:
43+
//
44+
// template <typename Callable, typename... Args>
45+
// using CallableTest =
46+
// decltype(std::declval<Callable>()(std::declval<Args>()...));
47+
//
48+
// template <typename Callable, typename... Args>
49+
// using IsCallable = IsDetected<CallableTest, Callable, Args...>;
50+
//
51+
template <template <typename...> class Trait, typename... Args>
52+
using IsDetected = detail::IsDetectedType<Trait, void, Args...>;
53+
54+
} // namespace nop
55+
56+
#endif // LIBNOP_INCLUDE_NOP_TRAITS_IS_DETECTED_H_

0 commit comments

Comments
 (0)