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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:41:44+00:00 2026-05-29T03:41:44+00:00

The following code (condensed from a larger program) does not compile with clang or

  • 0

The following code (condensed from a larger program) does not compile with clang or gcc.

struct S1 {
  void m1() {}
};

template<typename B> struct S2 : B {
  void m2() {}
  void m3();
};

template<typename S, void (S::*m)()> void f1(S* o) {
  (o->*m)();
}

template<typename B> void S2<B>::m3() {
  f1<S2, &S2::m1>(this);
}

int main() {
  void (S2<S1>::*m)() = &S2<S1>::m1;
  S2<S1> o;
  o.m3();
}

Here is clang’s error message:

bad.cc:15:3: error: no matching function for call to 'f1'
  f1<S2, &S2::m1>(this);
  ^~~~~~~~~~~~~~~
bad.cc:21:5: note: in instantiation of member function 'S2<S1>::m3' requested
      here
  o.m3();
    ^
bad.cc:10:43: note: candidate template ignored: invalid explicitly-specified
      argument for template parameter 'm'
template<typename S, void (S::*m)()> void f1(S* o) {
                                          ^
1 error generated.

This code compiles when I replace m1 by m2. Clearly the compiler knows about m1 (different message when I replace m1 by m4), so why should a pointer to it be invalid in this context?

  • 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-29T03:41:48+00:00Added an answer on May 29, 2026 at 3:41 am

    The thing is, the type of m1 is void(S1::*)(void), not void(S2::*)(void). So fix it by leveraging the known base class name:

    struct S1 {
        void m1() {}
    };
    
    template<typename B> struct S2 : B {
        void m2() {}
        void m3();
    };
    
    template<typename S, typename B, void (B::*m)(void)> void f1(S* o) {
        (o->*m)();
    }
    
    template<typename B> void S2<B>::m3() {
        f1<S2, B, &B::m1>(this);
    }
    
    int main() {
        S2<S1> o;
        o.m3();
    }
    

    Of course this doesn’t (yet) scale to methods defined in indirect base classes, but with a bit of TMP it can be done (will see if I can post that while the intermission of Going Native 2012 lasts :))

    The more ‘flexible’ approach would be:

    template<typename B, typename MF> void f1(B* o, MF mfp) {
        (o->*mfp)();
    }
    
    template<typename B> void S2<B>::m3() {
        f1(this, &B::m1);
    }
    

    You could/should use typetraits to ensure that S2<B>& is convertible to a B& if the class layout doesn’t already explicitly guarantee that, as in your current example.

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

Sidebar

Related Questions

The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code: template <typename S, typename T> struct foo { void bar(); };
Following code does compile in gcc 4.5 but it is not being compiled in
Consider the following condensed code: /* Compile: gcc -pthread -m32 -ansi x.c */ #include
Following code does compile whereas name aNumber is not declared before use. class A
I've managed to condense my confusion to the following code: template<typename T> class BTI
Following code does NOT work, but it expresses well what I wish to do.
The following code works great in IE, but not in FF or Safari. I
The following code should find the appropriate project tag and remove it from the
following code is used to find url from a string with php. Here is

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.