2

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?

2 Answers 2

1

You cannot use std::tuple::operator= because it's not constexpr. Your example can be converted to:

constexpr std::tuple<int, int> ret2 () {
    int a = 1;
    int b = 2;
    return std::make_tuple(a, b);
}

constexpr int ret1 () {
    constexpr auto t = ret2();
    return std::get<0>(t) + std::get<1>(t);
}

constexpr auto tmp = ret1();

which does the same as the original, though I'm not sure if it satisfies your real needs.

5
  • constexpr auto tmp = ret1(); does not directly use any operator=: it is an initialization, not an assignment expression.
    – aschepler
    Oct 25, 2017 at 23:10
  • I changed the code to show the need for both values. In my real use case, I need both values and they both help to produce the constexpr result.
    – nachum
    Oct 25, 2017 at 23:42
  • Is it possible to do this in one call?
    – nachum
    Oct 25, 2017 at 23:45
  • @nachum: updated. anyway it's constexpr so it wasn't a big deal to call it twice Oct 25, 2017 at 23:46
  • Perfect. Thanks!
    – nachum
    Oct 25, 2017 at 23:49
1

The implementation of your ret1 just needs std::get:

constexpr int ret1() {
    return std::get<0>(ret2());
}

Demo.

1
  • I changed the code to show the need for both values. In my real use case, I need both values and they both help to produce the constexpr result.
    – nachum
    Oct 25, 2017 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.