Optional is awesome and I'd like to return an optional<int> from a constexpr function. The following function compiles with g++ 5.2.0, but not with clang 3.7.0.
$ clang++ -std=c++1z -x c++ -c - <<< '
#include "optional.hpp"
constexpr std::experimental::optional<int> f1() { return 5; }
'
<stdin>:3:44: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr std::experimental::optional<int> f1() { return 5; }
^
<stdin>:3:51: note: non-constexpr constructor 'optional' cannot be used in a constant expression
constexpr std::experimental::optional<int> f1() { return 5; }
^
./optional.hpp:424:3: note: declared here
optional(optional&& rhs) noexcept(is_nothrow_move_constructible<T>::value)
^
I found a mention of this in the rationale:
[..] we know an efficient implementation of such optional with compile-time interface — except for copy constructor and move constructor — is possible.
If optionals can't be returned, their usefulness in constexpr functions seems unfortunately very limited to me.
As a temporary workaround, I tried adding constexpr to the move constructor, and both g++ and clang accepted it. Has the definition of constexpr functions been relaxed so that this is possible, or am I relying on overly permissible compilers?
Optional is awesome and I'd like to return an
optional<int>from a constexpr function. The following function compiles with g++ 5.2.0, but not with clang 3.7.0.I found a mention of this in the rationale:
If optionals can't be returned, their usefulness in constexpr functions seems unfortunately very limited to me.
As a temporary workaround, I tried adding
constexprto the move constructor, and both g++ and clang accepted it. Has the definition of constexpr functions been relaxed so that this is possible, or am I relying on overly permissible compilers?