Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
19d95ef0
Commit
19d95ef0
authored
Jun 04, 2016
by
Wenzel Jakob
Browse files
Merge pull request #226 from dean0x7d/constexpr_arg_check
Check the number of named arguments at compile time
parents
e3f8cfcb
9e62558d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
6 deletions
+16
-6
include/pybind11/attr.h
include/pybind11/attr.h
+13
-0
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+3
-6
No files found.
include/pybind11/attr.h
View file @
19d95ef0
...
...
@@ -321,5 +321,18 @@ template <typename... Args> struct process_attributes {
}
};
/// Compile-time integer sum
constexpr
size_t
constexpr_sum
()
{
return
0
;
}
template
<
typename
T
,
typename
...
Ts
>
constexpr
size_t
constexpr_sum
(
T
n
,
Ts
...
ns
)
{
return
n
+
constexpr_sum
(
ns
...);
}
/// Check the number of named arguments at compile time
template
<
typename
...
Extra
,
size_t
named
=
constexpr_sum
(
std
::
is_base_of
<
arg
,
Extra
>
::
value
...),
size_t
self
=
constexpr_sum
(
std
::
is_same
<
is_method
,
Extra
>::
value
...)
>
constexpr
bool
expected_num_args
(
size_t
nargs
)
{
return
named
==
0
||
(
self
+
named
)
==
nargs
;
}
NAMESPACE_END
(
detail
)
NAMESPACE_END
(
pybind11
)
include/pybind11/pybind11.h
View file @
19d95ef0
...
...
@@ -72,6 +72,9 @@ protected:
/// Special internal constructor for functors, lambda functions, etc.
template
<
typename
Func
,
typename
Return
,
typename
...
Args
,
typename
...
Extra
>
void
initialize
(
Func
&&
f
,
Return
(
*
)(
Args
...),
const
Extra
&
...
extra
)
{
static_assert
(
detail
::
expected_num_args
<
Extra
...
>
(
sizeof
...(
Args
)),
"The number of named arguments does not match the function signature"
);
struct
capture
{
typename
std
::
remove_reference
<
Func
>::
type
f
;
};
/* Store the function including any extra state it might have (e.g. a lambda capture object) */
...
...
@@ -206,12 +209,6 @@ protected:
}
#endif
if
(
!
rec
->
args
.
empty
()
&&
(
int
)
rec
->
args
.
size
()
!=
args
)
pybind11_fail
(
"cpp_function(): function
\"
"
+
std
::
string
(
rec
->
name
)
+
"
\"
takes "
+
std
::
to_string
(
args
)
+
" arguments, but "
+
std
::
to_string
(
rec
->
args
.
size
())
+
" pybind11::arg entries were specified!"
);
rec
->
signature
=
strdup
(
signature
.
c_str
());
rec
->
args
.
shrink_to_fit
();
rec
->
is_constructor
=
!
strcmp
(
rec
->
name
,
"__init__"
)
||
!
strcmp
(
rec
->
name
,
"__setstate__"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment