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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:24:48+00:00 2026-05-22T18:24:48+00:00

I have an almost working solution. However, it fails to compile some simple cases,

  • 0

I have an almost working solution. However, it fails to compile some simple cases, and I can’t decipher the error message.

My current solution:

#define AUTO_RETURN( EXPR ) -> decltype( EXPR ) \
{ return EXPR; }

template< typename BinaryFunc, typename First, typename Second >
auto foldl( BinaryFunc&& func, First&& first, Second&& second )
AUTO_RETURN( func( std::forward<First>(first), std::forward<Second>(second) ) )

template<typename BinaryFunc, typename First, typename Second, typename... Rest >
auto foldl( BinaryFunc&& func, First&& first, Second&& second, Rest&&... rest )
AUTO_RETURN(
   foldl(
      std::forward<BinaryFunc>(func),
      func( std::forward<First>(first), std::forward<Second>(second) ),
      std::forward<Rest>(rest)... )
   )

This works as expected:

struct adder
{
   template< int LHS, int RHS >
   std::integral_constant<int,LHS+RHS>
   operator()( std::integral_constant<int,LHS>, std::integral_constant<int,RHS> )
   {
      return {};
   }
};

auto result = foldl( adder(),
      std::integral_constant<int,19>{},
      std::integral_constant<int,23>{}
   );

assert( result.value == 42 );

However this fails to compile.

foldl( adder(),
      std::integral_constant<int,1>{},
      std::integral_constant<int,2>{},
      std::integral_constant<int,3>{},
      std::integral_constant<int,4>{},
   );

Oddly, if I remove all std::forward and rvalue refs from the code it works fine.

What am I doing wrong?
Is this a compiler bug?

  • 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-22T18:24:49+00:00Added an answer on May 22, 2026 at 6:24 pm

    The problem is that the decltype in the return type of the variadic foldl cannot see the variadic foldl, as it hasn’t been fully declared yet, so it cannot recurse to another instantiation of itself.

    You can do it with a helper struct:

    template<typename BinaryFunc, typename... Args >
    struct folder;
    
    template<typename BinaryFunc, typename First, typename Second>
    struct folder<BinaryFunc,First,Second>
    {
        static auto foldl( BinaryFunc&& func, First&& first, Second&& second )
            AUTO_RETURN( func( std::forward<First>(first), std::forward<Second>(second) ) )
    };
    
    template<typename BinaryFunc, typename First, typename Second, typename... Rest >
    struct folder<BinaryFunc,First,Second,Rest...>
    {
        static auto foldl(BinaryFunc&& func, First&& first, Second&& second, Rest&&... rest )
            AUTO_RETURN(
                (folder<
                BinaryFunc,
                 decltype(func( std::forward<First>(first), std::forward<Second>(second) )),Rest...>::
                foldl(
                    std::forward<BinaryFunc>(func),
                    func( std::forward<First>(first), std::forward<Second>(second) ),
                    std::forward<Rest>(rest)... )
                    ))
    };
    
    template< typename BinaryFunc, typename... Args >
    auto foldl( BinaryFunc&& func, Args&& ... args )
    AUTO_RETURN(
        (folder<BinaryFunc,Args...>::foldl(
          std::forward<BinaryFunc>(func),
          std::forward<Args>(args)... ))
       )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AudioQueue based application working almost perfectly. I'm suffering an issue, however.
I am about to start a web project and have been working almost exclusively
Some development skills, like refactoring operations, feel like they have an almost unlimited pontential
I have almost the worst spec in the world. An application that exists and
I have several input and option elements on my page, each (well almost) have
I have a table with almost 800,000 records and I am currently using dynamic
In the STL almost all containers have an erase function. The question I have
I have two insert statements, almost exactly the same, which run in two different
I have a page with a tab control and each control has almost 15
I have the following code in my Site.Master page of an almost empty ASP.NET

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.