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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:48:41+00:00 2026-06-03T01:48:41+00:00

I would very much like to be able to provide a functor as a

  • 0

I would very much like to be able to provide a functor as a template argument. The functors must be able to provide “themself” as that argument.

I imagine something like this:

template<typename T, template<typename> class SumFunctor> class foo;

template<typename T>
struct sum_default
{
    foo<T, sum_default> operator()(foo<T, sum_default> a, foo<T, sum_default> b) const 
    {
            T a_data = a.data();
            T b_data = b.data();
            return foo<T, sum_default>(a_data + b_data);
    }
};

template<typename T>
struct sum_awesome
{
    foo<T, sum_awesome> operator()(foo<T, sum_awesome> a, foo<T, sum_awesome> b) const 
    {
            T a_data = a.data();
            T b_data = b.data();
            return foo<T, sum_awesome>(a_data - b_data);
    }
};

template<typename T=int, template<typename> class SumFunctor=sum_default>
class foo
{
private:
    T _data;
    SumFunctor<T> _functor;
public:
    foo<T, SumFunctor>(T data) : _data(data) {}

    T data() { return _data; }

    friend const foo operator +(const foo& lhs, const foo& rhs)
    {
            return lhs._functor(lhs,rhs);
    }
};

int main(){
    foo<> a(42); 
    foo<double> b(1.0);
    foo<double,sum_default> c(4.0);
    foo<double,sum_awesome> d(4.0);

    a+a;
    d+d;
}

Is this possible, and if so, how?

An alternative solution is to provide the functor to the constructor, but this is very ugly i think, as the user must dynamically allocate the functor himself (As we cannot determine the type of the functor in the constructor. Using RTTI to do so also seems a bit ugly):

foo<double> a(42, new sum_default<double>() );

This also forces all functors to be derived from some pre-defined base functor.

UPDATE

Attempting to add template arguments to the sum_default template argument does not appear to solve the problem:

template<typename T>
struct sum_default
{
// Error    1   error C3200: 'sum_default<T>' : invalid template argument for template parameter 'SumFunctor', expected a class template
foo<T, sum_default<T> > operator()(foo<T, sum_default<T> > a, foo<T, sum_default<T> > b) const 
{
    T a_data = a.data();
    T b_data = b.data();
    return foo<T, sum_default<T> >(a_data + b_data);
}
};
  • 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-03T01:48:42+00:00Added an answer on June 3, 2026 at 1:48 am

    What’s biting you here is known as “class name injection” – inside of a class template, e.g. Foo<T>, unqualified use of Foo is actually treated as Foo<T>. Citing C++11 §14.6.1/1:

    Like normal (non-template) classes, class templates have an injected-class-name. The injected-class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type-specifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.

    Consequently, inside of sum_default<T>, when you have foo<T, sum_default>, it’s treated as though you typed foo<T, sum_default<T> > (which obviously won’t work as foo wants a template template parameter).

    In order to avoid this behavior, you need to qualify uses of the class template names inside of those class templates. Because your class templates are in the global scope, :: is sufficient:

    template<typename T>
    struct sum_default;
    
    template<typename T = int, template<typename> class SumFunctor = sum_default>
    class foo
    {
        T _data;
        SumFunctor<T> _functor;
    
    public:
        foo<T, SumFunctor>(T data) : _data(data) { }
    
        T data() { return _data; } const
    
        friend foo operator +(foo const& lhs, foo const& rhs)
        {
            return lhs._functor(lhs, rhs);
        }
    };
    
    template<typename T>
    struct sum_default
    {
        foo<T, ::sum_default> operator ()(foo<T, ::sum_default> a,
                                          foo<T, ::sum_default> b) const
        {
            return foo<T, ::sum_default>(a.data() + b.data());
        }
    };
    
    template<typename T>
    struct sum_awesome
    {
        foo<T, ::sum_awesome> operator()(foo<T, ::sum_awesome> a,
                                         foo<T, ::sum_awesome> b) const
        {
            return foo<T, ::sum_awesome>(a.data() - b.data());
        }
    };
    
    int main()
    {
        foo<> a(42);
        foo<double> b(1.0);
        foo<double, sum_default> c(4.0);
        foo<double, sum_awesome> d(4.0);
    
        a + a;
        d + d;
    }
    

    Note that this also allows you to define foo‘s constructor thusly, reducing a bit of noise:

    foo(T data) : _data(data) { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a complex setup/installer application in native C++/MFC. I would very much like
I'm creating a SharePoint publishing master page and would very much like to be
I have a very simple WCF service that I would like to expose publicly.
Would very much appreciate any help or hint on were to go next. I'm
have small problem, and would very much appreciate help :) I should convert byte
I'm a novice-to-intermediate JavaScript/jQuery programmer, so concrete/executable examples would be very much appreciated. My
I am looking for an idea, concept or proven datastructure that would be very
I'm in need of a distributed file system that must scale to very large
I have Camel route that I would like to expose as a REST Web
Okay, I have a multi-dimensional array which is statically-allocated. I'd very much like to

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.