Skip to content

Commit 25108a5

Browse files
committed
Add user-defined literal _a as a shortcut for
1 parent 65ebafd commit 25108a5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

include/eigenpy/fwd.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ struct has_operator_equal_impl {
196196
template <class T1, class T2 = T1>
197197
struct has_operator_equal : internal::has_operator_equal_impl<T1, T2>::type {};
198198

199+
namespace literals {
200+
/// \brief A string literal returning a boost::python::arg.
201+
///
202+
/// Using-declare this operator or do `using namespace eigenpy::literals`. Then
203+
/// `bp::arg("matrix")` can be replaced by the literal `"matrix"_a`.
204+
inline boost::python::arg operator"" _a(const char *name, std::size_t) {
205+
return boost::python::arg(name);
206+
}
207+
} // namespace literals
208+
199209
} // namespace eigenpy
200210

201211
#include "eigenpy/alignment.hpp"

unittest/user_type.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> build_matrix(int rows,
150150
template <typename Scalar>
151151
void expose_custom_type(const std::string& name) {
152152
using namespace Eigen;
153+
using eigenpy::literals::operator"" _a;
153154
namespace bp = boost::python;
154155

155156
typedef CustomType<Scalar> Type;
156157

157-
bp::class_<Type>(name.c_str(), bp::init<Scalar>(bp::arg("value")))
158+
// use ""_a literal
159+
bp::class_<Type>(name.c_str(), bp::init<Scalar>("value" _a))
158160

159161
.def(bp::self + bp::self)
160162
.def(bp::self - bp::self)

0 commit comments

Comments
 (0)