After searching for a while, I still can't quite get this. Can someone please tell me how to return a tuple in a constant expression. Here's the code:
#include <tuple>
constexpr std::tuple<int, int> ret2 () {
int a = 1;
int b = 2;
return std::make_tuple(a, b);
}
constexpr int ret1 () {
int a = 0;
int b = 0;
std::tie(a, b) = ret2();
return a + b;
}
constexpr auto tmp = ret1();
clang++ -std=c++14 -o test test.cpp
test.cpp:15:16: error: constexpr variable 'tmp' must be initialized by a constant expression
constexpr auto tmp = ret1();
^ ~~~~~~
test.cpp:11:17: note: non-constexpr function
'operator=<std::__1::tuple<int, int>, void>' cannot be used in a constant expression
std::tie(a, b) = ret2();
^
test.cpp:15:22: note: in call to 'ret1()'
constexpr auto tmp = ret1();
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/tuple:899:9: note:
declared here
operator=(_Tuple&& __t) _NOEXCEPT_((is_nothrow_assignable<base&...
^
1 error generated.
clang --version
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin17.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I have tried changing this to std::tuple<int&, int&>, and using std::ref, etc... I just can't seem to find the correct sequence.
Also, calling std::get 2x would be verbose in code and possibly wasteful in runtime. Is there some way to get both values through one call - as shown?