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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:43:04+00:00 2026-06-08T20:43:04+00:00

It is known that template arguments can be pointers to member functions. So I

  • 0

It is known that template arguments can be pointers to member functions.

So I can write:

struct Bar
{
    int fun(float x);
};

template <int (Bar::*FUN)(float)>
struct Foo
{ /*...*/ };

typedef Foo<&Bar::fun> FooBar;

But what if I want the the Bar type itself to be a template argument:

template <typename B, int (B::*FUN)(float)>
struct Foo
{ /*...*/ };

typedef Foo<Bar, &Bar::fun> FooBar;

Now, when I use it, I have to write Bar twice!

My question is: Is there a way to force the compiler to deduce the class type automatically?

The objective is for this to just work:

typedef Foo<&Bar::fun> FooBar;
typedef Foo<&Moo::fun> FooMoo;
  • 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-08T20:43:07+00:00Added an answer on June 8, 2026 at 8:43 pm

    You probably should just write the class name in there. However, if you really want to avoid that you can use the evil magic of macros. The simple version is more dangerous:

    #define TT(X) decltype(X), X
    
    template<typename T,T t>
    struct Foo
    { /* ... */ };
    
    struct Bar {
        int fun(float) {}
    };
    
    int main() {
        Foo<TT(&Bar::fun)> f;
    }
    

    This will accept any kind of non-type template parameter, and you may encounter hard-to-understand errors if the Foo implementation only works with pointers-to-member.

    To make it a bit safer you need a metafunction that tells you the class name:

    template<typename T> struct member_ptr_traits;
    
    template<typename Class,typename Ret,typename... Args>
    struct member_ptr_traits<Ret (Class::*)(Args...)>
    {
        typedef Class class_type;
        typedef Ret return_type;
    };
    
    #define TT(X) member_ptr_traits<decltype(X)>::class_type , X
    
    template<typename T,int (T::*FUN)(float)>
    struct Foo
    { /* ... */ };
    
    struct Bar {
        int fun(float) {}
    };
    
    int main() {
        Foo<TT(&Bar::fun)> f;
    }
    

    Also both of these use C++11 so they won’t work with old compilers. This simple version can be rewritten to use the old typeof or similar compiler extensions. Rewriting the safer version requires simulating variadic templates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can press shift+alt+j to insert an appropriate comment template for
I known that with subversion you can attach arbitrary properties to each files. In
I got class with template methods that looks at this: struct undefined {}; template<typename
I know that when we are using template inside another template, we should write
I want to write several functions that are only different in the types of
I've found that when accessing a non-template attribute ( v.foo ) from a variable
I know that non-type template argument for intgral type must be const expression so:
Its known that Java ArrayList is implemented using arrays and initializes with capacity of
Background It is known that random branching costs significant overhead. And there was a
It's known that CF indicates unsigned carry out and OF indicates signed overflow. So

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.