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

  • Home
  • SEARCH
  • 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 8997607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:54:44+00:00 2026-06-15T23:54:44+00:00

In my last question I received great help in getting a template specialization to

  • 0

In my last question I received great help in getting a template specialization to work. Now I need a little extension. I want two specializations for these statements:

int main()
{
    // First specialization
    holder_ext<person> h1;
    holder_ext<person, &person::age> h2;
    holder_ext<int> h3;

    // Second specialization
    holder_ext<person, &person::age, &person::name> h4;
}

My class person looks like this:

class person
{
private:
    std::string name_;
    int age_;
public:
    person(const std::string &name)
        : name_(name), age_(56)
    {}
    void age(int a) { age_ = i; }
    void name(const std::string &n) { name_ = n; }
};

The special thing is, that the two member functions have different parameter types. So I can’t use the same variadic template member function for both. I tried it with two different variadic templates. But that doesn’t work. Also default values for the member functions do not work.

Does anybody have a good hint for me?

This is the solution with one member function (thanks to Pubby):

template < class T, void (std::conditional<std::is_class<T>::value, T, struct dummy>::type::* ...FUNC)(int)> class holder;

template < class T, void (T::*FUNC)(int)>
class holder<T, FUNC>
{
public:
    explicit holder() : setter(FUNC) { std::cout << "func\n"; }
private:
    std::function<void (value_type&, int)> setter;
};

template < class T>
class holder<T>
{
public:
    explicit holder() { std::cout << "plain\n"; }
};

Thanks again in advance!

P.S.: And no, I won’t come up in two days with “what must do with three, four, five member functions”? 😉

  • 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-15T23:54:45+00:00Added an answer on June 15, 2026 at 11:54 pm

    Finally I found a solution for my problem. It is a mix between variadic templates and template specilization:

    template < class T,
    void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC1)(int) = nullptr,
    void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::* ...FUNC2)(const std::string&)
    >
    class holder_ext;
    
    template < class T,
    void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC1)(int),
    void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC2)(const std::string&)
    >
    class holder_ext<T, FUNC1, FUNC2>
    {
    public:
        holder_ext() { std::cout << "func 2 test\n"; }
    };
    
    template < class T,
    void (std::conditional<std::is_base_of<object, T>::value, T, struct dummy>::type::*FUNC1)(int)
    >
    class holder_ext<T, FUNC1>
    {
    public:
        holder_ext() { std::cout << "func 1 test\n"; }
    };
    

    I use a not implemented declaration and define two specializations. One with both member function and the other one for all other cases.

    If there is a better solution dont’t hesitate to tell me.

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

Sidebar

Related Questions

Ok, I asked a question last night and received a number of really great
My last question was about getting the string representation of an object serialized to
Following from my last question which @Jon Skeet gave me a lot of help
From my last question , I now know how to get access to the
I received some justified critical feedback on my last question ( How to gracefully
So, in my last question I asked for help in parsing the links from
My last question wasn't explained very well. What I'm trying to do here is
My last question was somewhat narrower than this one. I am interested to know
After having my last question answered , I have never see the preventDefault(); function
Let me rephrase my last question, what PHP library or framework can I use

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.