#include <tuple>
int main() {
static_assert(std::is_same<std::tuple<int&&>,
decltype(std::forward_as_tuple(1))>::value, "");
constexpr int x = 5;
constexpr auto t1 = std::forward_as_tuple(1); // (1)
constexpr auto t2 = std::forward_as_tuple(x); // (2)
constexpr std::tuple<int&&> t3(1); // (3)
constexpr std::tuple<int> t4(1); // OK!
}
In the above code, that static_assert passes, however lines 1 through 3 fail to compile with both gcc 4.9 (supplied by ubuntu), and clang. They complain that the variables are not initialized by constexprs
, that x
is not a constexpr
(even though it's initialized by a literal), that is creates a reference to a temporary or that their implementation of forward_as_tuple()
is not (though the C++14 standard does guarantee this).
I'm working on some code that makes heavy use of std::tuple
and constexpr
. I can get around std::forward_as_tuple()
not being defined as constexpr
, but I can't understand why forward_as_tuple(0)
would return a tuple<int&&>
, which according to clang creates a reference to a temporary making it not constexpr. The alternatives don't work for what I need--std::make_tuple()
can't be used for perfect forwarding and std::tie
can't store literal values. Edit: Why does std::forward_as_tuple()
work this way and without offering an alternative?
Am I doing something fundamentally wrong here or is there something I don't understand?