Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
19d95ef0
Commit
19d95ef0
authored
Jun 04, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #226 from dean0x7d/constexpr_arg_check
Check the number of named arguments at compile time
parents
e3f8cfcb
9e62558d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
6 deletions
+16
-6
include/pybind11/attr.h
+13
-0
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 {
...
@@ -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
(
detail
)
NAMESPACE_END
(
pybind11
)
NAMESPACE_END
(
pybind11
)
include/pybind11/pybind11.h
View file @
19d95ef0
...
@@ -72,6 +72,9 @@ protected:
...
@@ -72,6 +72,9 @@ protected:
/// Special internal constructor for functors, lambda functions, etc.
/// Special internal constructor for functors, lambda functions, etc.
template
<
typename
Func
,
typename
Return
,
typename
...
Args
,
typename
...
Extra
>
template
<
typename
Func
,
typename
Return
,
typename
...
Args
,
typename
...
Extra
>
void
initialize
(
Func
&&
f
,
Return
(
*
)(
Args
...),
const
Extra
&
...
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
;
};
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) */
/* Store the function including any extra state it might have (e.g. a lambda capture object) */
...
@@ -206,12 +209,6 @@ protected:
...
@@ -206,12 +209,6 @@ protected:
}
}
#endif
#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
->
signature
=
strdup
(
signature
.
c_str
());
rec
->
args
.
shrink_to_fit
();
rec
->
args
.
shrink_to_fit
();
rec
->
is_constructor
=
!
strcmp
(
rec
->
name
,
"__init__"
)
||
!
strcmp
(
rec
->
name
,
"__setstate__"
);
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