diff --git a/libuavcan/include/uavcan/internal/lazy_constructor.hpp b/libuavcan/include/uavcan/internal/lazy_constructor.hpp index 03bd2963ce..25c3f8cbfe 100644 --- a/libuavcan/include/uavcan/internal/lazy_constructor.hpp +++ b/libuavcan/include/uavcan/internal/lazy_constructor.hpp @@ -39,6 +39,12 @@ class LazyConstructor failure(); } + template struct ParamType { typedef const U& Type; }; + template struct ParamType { typedef U& Type; }; +#if __cplusplus > 201100 + template struct ParamType { typedef U&& Type; }; +#endif + public: LazyConstructor() : ptr_(NULL) @@ -51,7 +57,7 @@ public: { std::fill(data_, data_ + sizeof(T), 0); if (rhs) - construct(*rhs); // Invoke copy constructor + construct(*rhs); // Invoke copy constructor } ~LazyConstructor() { destroy(); } @@ -60,7 +66,7 @@ public: { destroy(); if (rhs) - construct(*rhs); // Invoke copy constructor + construct(*rhs); // Invoke copy constructor return *this; } @@ -98,47 +104,50 @@ public: // for nargs in range(1, MAX_ARGS + 1): // nums = [(x + 1) for x in range(nargs)] // l1 = ['typename P%d' % x for x in nums] -// l2 = ['P%d p%d' % (x, x) for x in nums] +// l2 = ['typename ParamType::Type p%d' % (x, x) for x in nums] // l3 = ['p%d' % x for x in nums] // print(TEMPLATE % (', '.join(l1), ', '.join(l2), ', '.join(l3))) template - void construct(P1 p1) + void construct(typename ParamType::Type p1) { ensureNotConstructed(); ptr_ = new (static_cast(data_)) T(p1); } template - void construct(P1 p1, P2 p2) + void construct(typename ParamType::Type p1, typename ParamType::Type p2) { ensureNotConstructed(); ptr_ = new (static_cast(data_)) T(p1, p2); } template - void construct(P1 p1, P2 p2, P3 p3) + void construct(typename ParamType::Type p1, typename ParamType::Type p2, typename ParamType::Type p3) { ensureNotConstructed(); ptr_ = new (static_cast(data_)) T(p1, p2, p3); } template - void construct(P1 p1, P2 p2, P3 p3, P4 p4) + void construct(typename ParamType::Type p1, typename ParamType::Type p2, typename ParamType::Type p3, + typename ParamType::Type p4) { ensureNotConstructed(); ptr_ = new (static_cast(data_)) T(p1, p2, p3, p4); } template - void construct(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) + void construct(typename ParamType::Type p1, typename ParamType::Type p2, typename ParamType::Type p3, + typename ParamType::Type p4, typename ParamType::Type p5) { ensureNotConstructed(); ptr_ = new (static_cast(data_)) T(p1, p2, p3, p4, p5); } template - void construct(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) + void construct(typename ParamType::Type p1, typename ParamType::Type p2, typename ParamType::Type p3, + typename ParamType::Type p4, typename ParamType::Type p5, typename ParamType::Type p6) { ensureNotConstructed(); ptr_ = new (static_cast(data_)) T(p1, p2, p3, p4, p5, p6); diff --git a/libuavcan/test/lazy_constructor.cpp b/libuavcan/test/lazy_constructor.cpp index 42d9ff0b43..56f7ebcb0a 100644 --- a/libuavcan/test/lazy_constructor.cpp +++ b/libuavcan/test/lazy_constructor.cpp @@ -21,7 +21,7 @@ TEST(LazyConstructor, Basic) */ a.destroy(); // no-op a.construct(); - b.construct("Hello world"); + b.construct("Hello world"); ASSERT_TRUE(a); ASSERT_TRUE(b.isConstructed());