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 8208889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:31:33+00:00 2026-06-07T09:31:33+00:00

I’m writing an implementation of zip , but I’ve ran into a bit of

  • 0

I’m writing an implementation of zip, but I’ve ran into a bit of a problem. Here’s a minimal test case:

#include <iostream>
#include <deque>
#include <tuple>
#include <string>
#include <limits>

template <template <typename...> class Container, typename... Types>
Container<std::tuple<Types...>> zip(Container<Types> const&... args) {
  unsigned len = commonLength(args...);
  Container<std::tuple<Types...>> res;
  std::tuple<Types...> item;

  for (unsigned i=0; i<len; i++) {
    item = getTupleFrom(i, args...);
    res.push_back(item);
  }

  return res;
}

template <class ContainerA, class... Containers>
unsigned commonLength(ContainerA first, Containers... rest, unsigned len=std::numeric_limits<unsigned>::max()) {
  unsigned firstLen = first.size();
  if (len > firstLen) {
    len = firstLen;
  }
  return commonLength(rest..., len);
}

template <class ContainerA>
unsigned commonLength(ContainerA first, unsigned len=std::numeric_limits<unsigned>::max()) {
  unsigned firstLen = first.size();
  if (len > firstLen) {
    len = firstLen;
  }
  return len;
}

template <template <typename...> class Container, typename TypeA, typename... Types>
std::tuple<TypeA, Types...> getTupleFrom(unsigned index, Container<TypeA> const& first, Container<Types> const&... rest) {
  return std::tuple_cat(std::tuple<TypeA>(first[index]), getTupleFrom(index, rest...));
}

template <template <typename...> class Container, typename TypeA>
std::tuple<TypeA> getTupleFrom(unsigned index, Container<TypeA> const& first) {
  return std::tuple<TypeA>(first[index]);
}

int main() {

  std::deque<int> test1 = {1, 2, 3, 4};
  std::deque<std::string> test2 = {"hihi", "jump", "queue"};
  std::deque<float> test3 = {0.2, 8.3, 7, 123, 2.3};
  for (auto i : zip(test1, test2, test3)) {
    std::cout << std::get<0>(i) << std::get<1>(i) << std::get<2>(i) << std::endl;
  }
  //expected output:
  //1hihi0.2
  //2jump8.3
  //3queue7
  return 0;
}

When compiling I get the following error:

error: no matching function for call to ‘commonLength(const Star::List<int>&, const Star::List<std::basic_string<char> >&, const Star::List<float>&)’
note: candidates are:
note: template<class ContainerA, class ... Containers> unsigned int Star::commonLength(ContainerA, Containers ..., unsigned int)
note: template<class ContainerA> unsigned int Star::commonLength(ContainerA, unsigned int)

I’m assuming I’m specifying my template parameters wrong or something like that. I also attempted to restructure and eliminate that function altogether, but then I get the same error against getTupleFrom.

Can anyone explain to me why I’m stupid? Because I just don’t know what I’m doing wrong. 🙁

  • 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-07T09:31:34+00:00Added an answer on June 7, 2026 at 9:31 am

    Well, this works:

    #include <iostream>
    #include <deque>
    #include <tuple>
    #include <string>
    #include <type_traits>
    #include <algorithm>
    #include <limits>
    
    template <class ContainerA>
    unsigned commonLength(unsigned len, const ContainerA &first) {
      unsigned firstLen = first.size();
      if (len > firstLen) {
        len = firstLen;
      }
      return len;
    }
    
    
    template <class ContainerA, class... Containers>
    unsigned commonLength(unsigned len, const ContainerA &first, const Containers&... rest) {
      unsigned firstLen = first.size();
      if (len > firstLen) {
        len = firstLen;
      }
      return commonLength(len, rest...);
    }
    
    template <template <typename...> class Container, typename TypeA>
    std::tuple<TypeA> getTupleFrom(unsigned index, Container<TypeA> const& first) {
      return std::tuple<TypeA>(first[index]);
    }
    
    template <template <typename...> class Container, typename TypeA, typename... Types>
    std::tuple<TypeA, Types...> getTupleFrom(unsigned index, Container<TypeA> const& first, Container<Types> const&... rest) {
      return std::tuple_cat(std::tuple<TypeA>(first[index]), getTupleFrom(index, rest...));
    }
    
    template <template <typename...> class Container, typename... Types>
    Container<std::tuple<Types...>> zip(Container<Types> const&... args) {
      unsigned len = commonLength(std::numeric_limits<unsigned>::max(), args...);
      Container<std::tuple<Types...>> res;
      std::tuple<Types...> item;
    
      for (unsigned i=0; i<len; i++) {
        item = getTupleFrom(i, args...);
        res.push_back(item);
      }
    
      return res;
    }
    
    int main() {
    
      std::deque<int> test1 = {1, 2, 3, 4};
      std::deque<std::string> test2 = {"hihi", "jump", "queue"};
      std::deque<float> test3 = {0.2, 8.3, 7, 123, 2.3};
      for (auto i : zip(test1, test2, test3)) {
        std::cout << std::get<0>(i) << std::get<1>(i) << std::get<2>(i) << std::endl;
      }
      //expected output:
      //1hihi0.2
      //2jump8.3
      //3queue7
    }
    

    It outputs exactly what you expected. The problems were:

    • You weren’t using const& containers in commonLength, while zip‘s arguments where const references.
    • The unsigned parameter in commonLength coudln’t be deduced, so I moved it to the beginning
    • You declared/defined functions in the wrong order(A required B, but A was defined before B), so I reordered them.

    Apparently clang 3.1 fails to deduce the template arguments in zip, but g++ 4.6 gets them fine.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.