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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:32:46+00:00 2026-06-10T22:32:46+00:00

So, the more I think about this the weirder its legality seems. When I

  • 0

So, the more I think about this the weirder its legality seems. When I pass my an instance of my memoizer functor into its constructor, the compiler doesn’t complain. How should ‘f’ be read in the memoizer template? It seems that with the same type declaration OutputT (&f) InputT it can refer to a function by reference, or–and I thought this was illegal–a reference to a member function, namely operator() of an instance of the class memoizer<int,int>. Should f instead be read as ‘a reference to something with an operator() defined?’

#include <map>
#include <iostream>

template<typename OutputT, typename InputT>
class memoizer
{
private:
    OutputT (&f) (InputT);
    std::map<InputT,OutputT> dat;

public:
    memoizer( OutputT (&f) ( InputT ) ) : f(f) { }

    OutputT operator()( InputT t )
    {
        if( dat.count(t)==0 )
            dat[t] = f(t);
        return dat[t];
    }
};

int fib( int n )
{
    if( n < 2 ) return 1;
    return fib( n-2 ) + fib( n-1 );
}

int main()
{
    memoizer<int,int> fib_memo( fib );
    memoizer<int,int> fib_memo_memo( fib_memo );    

    std::cout << fib( 12 ) << "\n";
    std::cout << fib_memo( 12 ) << "\n";
    std::cout << fib_memo_memo( 12 ) << "\n";
}

I mean, don’t get me wrong: I’m glad it works, it just seems kind of ‘magical.’ Thanks in advance.

  • 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-10T22:32:47+00:00Added an answer on June 10, 2026 at 10:32 pm

    You’re tricking yourself. fib_memo_memo is not a memoizer whose f member variable contains fib_memo. Your initializer there is actually using the implicit copy constructor, so fib_memo_memo is just a copy of fib_memo. You can verify this yourself by adding

    memoizer(const memoizer &m) = delete;
    

    You’ll start getting errors like

    error: call to deleted constructor of 'memoizer<int, int>'
        memoizer<int,int> fib_memo_memo( fib_memo );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The more I delve into javascript, the more I think about the consequences of
Alright, this will probably sound a bit silly. The more I think about it,
I'm not a beginner, but the more I think about this issue, the more
[see later answer for more] I think this is just a simple rails question,
I know more or less how to do this, but I think I'm getting
This isn't about a side-by-side technical comparison, rather about how to think in jQuery
To make my extranet web application even faster/more scalable I think of using some
2 short questions based on trying to make my code more efficient (I think
I think one of the more difficult concepts to understand in the Zend Framework
I think it is quite normal to have more than one binary in a

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.