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 a code that has been working for almost 4 years (since boost
I'm working on a simple experiment in Python. I have a master process, in
I have a problem with grid view layout on Android. I can't find solution
I have almost the worst spec in the world. An application that exists and
I have almost 800 MB and over 31,000 files in hundreds of subfolders at
I admit that I have almost none experience of unittesting. I did a try
I am trying to make my own user control and have almost finished it,

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.