Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:24:44+00:00 2026-05-16T04:24:44+00:00

tuple in boost and TR1/c++0x provides a convenient (for the writer of the function)

  • 0

tuple in boost and TR1/c++0x provides a convenient (for the writer of the function) method to return two values from a function–however it seems to damage one major feature of the language for the caller: the ability to simply use the function to initialize a variable:

T happy();
const auto meaningful_name(happy()); // RVO means no excess copies

but for:

tuple<T,U> sad();

we either have to surrender the ability to pick a meaningful name for our return values, and use get<n>() everywhere:

const auto two_unrelated_things(sad());

or make a temporary:

const auto unwanted_named_temporary(sad());
const auto one_name(get<0>(unwanted_named_temporary));
const auto two_name(get<1>(unwanted_named_temporary));

or switch from initialization to assignment, which only works when the types are assignable, and breaks auto:

tuple_element<0, decltype(sad())>::type one_mutable; // there might be a less
tuple_element<1, decltype(sad())>::type two_mutable; // verbose way
tie(one_mutable,two_mutable) = sad();

or do something unnatural to a local class:

const struct ugh { 
  ugh( decltype(sad()) rhs ) : one_name(get<0>(rhs)), two_name(get<1>(rhs)) {}
  const tuple_element<0, decltype(sad())>::type one_name;
  const tuple_element<1, decltype(sad())>::type two_name;
} stuff(sad()); // at least we avoid the temporary and get initialization

Is there a better way? I’m using VC10 compatible constructs above, would anything in full c++0x or boost help?

Ideally it would:

  • allow me to use initialization, not just assignment
  • let the caller pick the names for the returned-into variables
  • not make extra copies
  • work for both stack variables and class members
  • possibly be a big crazy template library, but have sane syntax for caller and function writer
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:24:45+00:00Added an answer on May 16, 2026 at 4:24 am
    std::tuple<Type1, Type2> returnValue = sad();
    Type1& first = std::get<0>(returnValue);
    Type2& second = std::get<1>(returnValue);
    

    I’m not sure what your fourth bullet means, but that satisfies all the rest.

    *edit: Based on your comment above, I figured out what you meant by the fourth bullet.

    struct Object {
        Object(const std::tuple<Type1, Type2>& t) : value(t) { }
        Type1& First() { return std::get<0>(value); }
        Type2& second() { return std::get<1>(value); }
    private:
        std::tuple<Type1, Type2> value;
    }
    

    Modify as needed.

    You could also just not use std::tuple at all if the returned values are so unrelated that you have to split them up in order for them to be used reasonably. People have gotten by for years returned structs with reasonably named fields, or by accepting reference parameters for output.

    As an aside, you seem to be in love with auto. Don’t be. Its a great feature, but this is not the way it should to be used. Your code is going to end up illegible if you don’t specify types from time to time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I include both gtest/gtest.h and boost/math/distributions/poisson.hpp I get /opt/local/include/boost/tr1/tuple.hpp:63: error: ‘tuple’ is already
boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, foo,
I want to define std::tr1::hash<boost::tuple<A,B,C> > . But I get an error that doesn't
In the sample code below, it shows that boost::tuple can be created implicitly from
For example, this is the declaration of boost::tuple // - tuple forward declaration -----------------------------------------------
Trying to build the following simple example #include <boost/python.hpp> using namespace boost::python; tuple head_and_tail(object
#include <list> #include <boost/tuple/tuple.hpp> template<class InputIterator> void f(InputIterator it) { typedef boost::tuple<typename InputIterator::value_type, int>
visual studio tells me error C2664: 'boost::asio::mutable_buffer::mutable_buffer(const boost::asio::mutable_buffer&)': impossible to convert parameter 1 from
Is there're way to pass boost::tuple to printf()?
Why is returning a std::pair or boost::tuple so much less efficient than returning by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.