Reputation: 495
Question is really simple, why is this code not working:
#include <tuple>
int main( int argc, char* argv[]) {
const int a,b = std::tie(std::make_pair(1,2));
return EXIT_SUCCESS;
}
g++ gives me this error:
./test.cpp: In function ‘int main(int, char**)’: ./test.cpp:4:13: error: uninitialized const ‘a’ [-fpermissive] const int a,b = std::tie(std::make_pair(1,2)); ^ ./test.cpp:4:42:
error: cannot bind non-const lvalue reference of type ‘std::pair&’ to an rvalue of type ‘std::pair’
const int a,b = std::tie(std::make_pair(1,2));
I cannot get any tuple-like return by value, using this kind of pattern (either const or non const). Is it a better way to do what I am trying to achieve here ?
Upvotes: 2
Views: 711
Reputation: 93294
const int a,b = std::tie(...)
This isn't doing what you think it is. It's creating two const int
variables:
a
, uninitialized
b
, initialized to std::tie(...)
The proper way of using std::tie
is as follows:
int a, b;
std::tie(a, b) = std::make_pair(1, 2);
Note that you need a
and b
to be already declared and non-const
.
In C++17, you can use structured bindings instead:
const auto [a, b] = std::make_pair(1, 2);
Upvotes: 11