I'm working on an Optional<T>
class because I'm using c++11 and the compiler does not provide an implementation. I have problem with the rvalue reference constructor. The following constructors seems to work fine
Optional();
Optional(const ThisType& other);
Optional(ThisType&& other);
template <typename U>
Optional(const Optional<U>& other);
template <typename U>
Optional(Optional<U>&& other);
template <typename... Args>
explicit Optional(Unused, Args&&... args);
template <typename U, typename... Args>
explicit Optional(Unused, InitializerList<U> initList, Args&&... args);
template <typename U>
Optional(const U& value);
Where ValueType
is a typedef T
and ThisType
is a typedef Optional<T>
. However when I'm trying to implement an rvalue reference of U
constructor:
template <typename U>
Optional(U&& value);
then the following test case is not working:
// Optional<T>(const Optional<T>&)
Opt o(bmstl::Unused{});
Opt o2 = o;
The compiler tries to resolve this using the Optional<T>(U&&)
and it of course cannot convert Optional<T>
to T
, where Optional<T>
is Opt
and T
is Foo
error C2664: 'Foo::Foo(Foo &&)' : cannot convert argument 1 from 'Opt' to 'const Foo &'
If I change the test case from Opt o(bmstl::Unused{});
to const Opt o(bmstl::Unused{});
it works fine, but that's not a solution.
I've tried to use enable_if
on the rvalue constructor with no luck. I thought that I should enable this when the type of U
is not an Optional<U>
but probably I'm wrong.
template <typename U, typename = typename EnableIf<!IsSame<DecayT<U>, Optional<U>>::Value>::Type>
Optional(U&& value);
Aucun commentaire:
Enregistrer un commentaire