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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:19:05+00:00 2026-06-14T20:19:05+00:00

I implemented a C++ equivalent of Python’s chain function a while ago thanks to

  • 0

I implemented a C++ equivalent of Python’s chain function a while ago thanks to variadic templates. The function is used to iterate successively through many containers. Here is the old working version of the function using a generator named ChainedObject, whatever it is:

template<typename... Iterables>
auto chain(Iterables&&... iters)
    -> ChainObject<Iterables...>
{
    return /* ... */;
}

And the corresponding main:

int main()
{
    std::vector<int> vec = { 1, 2, 3, 4, 5 };
    std::list<int>   li  = { 6, 7, 8, 9, 10, 11, 12, 13 };
    for (auto& i: chain(vec, li))
    {
        // You can edit a range of iterables
        // as if there was only one of them.
        i *= 5;

        std::cout << i << std::endl;
    }
    return 0;
}

That main worked fine. We don’t care what there is in ChainObject for the problem, so let’s see it. I tried to use template templates to ensure that the different collections used had the same value_type and modified the function chain the following way:

template<typename T, template<typename...> class... Iterables>
auto chain(Iterables<T>&&... iters)
    -> ChainObject<T, Iterables...>
{
    return /* ... */;
}

I thought this would do the trick to ensure the list and vector from my previous main share a same type, but instead, I get the following error from GCC 4.7.1:

In function ‘int main()’:

error: no matching function for call to ‘chain(std::vector&, std::list&)’

note: candidates are:

note: ChainObject<T, Iterables ...> chain(Iterables<T>&& ...) [with T = int; Iterables = {std::vector, std::list}]

note: no known conversion for argument 2 from ‘std::list<int>‘ to ‘std::list<int>&&‘

note: ChainObject<T, Iterables ...> chain(Iterables<T>&& ...) [with T = int; Iterables = {std::vector, std::list}]

note: no known conversion for argument 2 from ‘std::list<int>‘ to ‘std::list<int>&&‘

error: unable to deduce ‘auto&’ from ”

It seems that the problem comes from argument passing to the function taking rvalue references. However, I really don’t understand why my first version worked fine, and note the one using template templates.

  • 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-14T20:19:07+00:00Added an answer on June 14, 2026 at 8:19 pm

    Your problem is that the T&& template magic only works for type parameters (it works by deducing T as eg. int& if needed – for lvalue arguments). It can’t work for template template arguments, where the actual type is X<T>&& – X must be a class template in this case, not something like “reference-to-class-template”. So in the end you have to pass a rvalue-reference, which you cannot implicitly get from a lvalue (variable).

    That said, I would suggest you to revert to your earlier code and check that the value_types are the same (or compatible, etc., whatever gets you going) with SFINAE.

    Rough code sketch (for strict equality):

    template <class ... Ts> struct all_types_equal
    {
      static const bool value = false;
    };
    
    template <class T>
    struct all_types_equal<T>
    {
      static const bool value = true;
    };
    template <class T, class ... Rest>
    struct all_types_equal<T, T, Rest...>
    {
      static const bool value = all_types_equal<T, Rest...>::value;
    };
    
    template<typename... Iterables>
    auto chain(Iterables&&... iters)
        -> typename std::enable_if<all_types_equal<Iterable::value_type...>::value, ChainObject<Iterables...> >::type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw that @GMan implemented a version of sizeof... for variadic templates which (as
Is there an equivalent function to this Python function in Java? struct.unpack(fmt, string) I'm
How is application pool implemented in IIS? Is each application pool equivalent to a
I've found this topic which I've implemented (see accepted answer): javascript equivalent of PHP's
We implemented In-app Billing one year ago with no problems following the sample code
Is there C++ equivalent for python Xrange generator in either STL or boost? xrange
Possible Duplicate: Set undefined javascript property before read Is there an equivalent of Python's
Is there an equivalent to Jack for Narhwal implemented in nodejs?
What is the idiomatic Python equivalent of this C/C++ code? void foo() { static
Is it possible to implement in Scala something equivalent to the Python yield statement

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.