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

  • SEARCH
  • Home
  • 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 7943505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:17:30+00:00 2026-06-04T00:17:30+00:00

If I have std::tuple<double, double, double> (where the type is homogeneous), is there a

  • 0

If I have std::tuple<double, double, double> (where the type is homogeneous), is there a stock function or constructor to convert to std::array<double>?

Edit:: I was able to get it working with recursive template code (my draft answer posted below). Is this the best way to handle this? It seems like there would be a stock function for this… Or if you have improvements to my answer, I’d appreciate it. I’ll leave the question unanswered (after all, I want a good way, not just a workable way), and would prefer to select someone else’s [hopefully better] answer.

Thanks for your advice.

  • 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-06-04T00:17:31+00:00Added an answer on June 4, 2026 at 12:17 am

    Converting a tuple to an array without making use of recursion, including use of perfect-forwarding (useful for move-only types):

    #include <iostream>
    #include <tuple>
    #include <array>
    
    template<int... Indices>
    struct indices {
        using next = indices<Indices..., sizeof...(Indices)>;
    };
    
    template<int Size>
    struct build_indices {
        using type = typename build_indices<Size - 1>::type::next;
    };
    
    template<>
    struct build_indices<0> {
        using type = indices<>;
    };
    
    template<typename T>
    using Bare = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
    
    template<typename Tuple>
    constexpr
    typename build_indices<std::tuple_size<Bare<Tuple>>::value>::type
    make_indices()
    { return {}; }
    
    template<typename Tuple, int... Indices>
    std::array<
      typename std::tuple_element<0, Bare<Tuple>>::type,
        std::tuple_size<Bare<Tuple>>::value
    >
    to_array(Tuple&& tuple, indices<Indices...>)
    {
        using std::get;
        return {{ get<Indices>(std::forward<Tuple>(tuple))... }};
    }
    
    template<typename Tuple>
    auto to_array(Tuple&& tuple)
    -> decltype( to_array(std::declval<Tuple>(), make_indices<Tuple>()) )
    {
        return to_array(std::forward<Tuple>(tuple), make_indices<Tuple>());
    }
    
    int main() {
      std::tuple<double, double, double> tup(1.5, 2.5, 4.5);
      auto arr = to_array(tup);
      for (double x : arr)
        std::cout << x << " ";
      std::cout << std::endl;
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable i of type std::size_t and a tuple of type std::tuple
I have a question about performance of move constructor, pseudo C++ class: typedef tuple<std::string,
boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, foo,
I have the following: #include<iostream> #include<unordered_map> #include<tuple> using namespace std; class CTest { //
I have a function in C++ that returns a vector<vector<double> > object. I have
Is there a difference between an std::pair and an std::tuple with only two members?
I have wrote the following code to get the offset of a tuple element
EDIT: Solved, my mistake; explained in my answer. I have this: std::vector < boost::shared_ptr
I have a std::set which is of type point struct point { int x;
let's say I have std::map< std::string, std::string > m_someMap as a private member variable

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.