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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:38:02+00:00 2026-06-05T09:38:02+00:00

I need to call different versions of a template member function with the same

  • 0

I need to call different versions of a template member function with the same arguments based on certain static members of the template parameters. Here’s a sort of simplified version of what I need to do:


class A {
public:
    //...
    static const char fooString[];
};
const char A::fooString[] = "This is a Foo.";

class B {
public:
    //...
    static const char barString[];
};
const char B::barString[] = "This is a Bar.";

class C {
public:
    //...
    static const char fooString[];
};
const char C::fooString[] = "This is also a Foo.";

//Many other classes which have either a fooString or a barString

void doFoo(const char*s) { /*something*/ }
void doBar(const char*s) { /*something else*/ }

template<class T>
class Something {
public:
    //This version should be called if T has a static member called "fooString",
    //so it should be called if T is either class A or C
    void doSomething() { doFoo(T::fooString); }

    //This version should be called if T has a static member called "barString", 
    //so it should be called if T is class B
    void doSomething() { doBar(T::barString); }
};


void someFunc()
{
    Something<A> a;
    Something<B> b;
    Something<C> c;

    a.doSomething();    //should call doFoo(A::fooString)
    b.doSomething();    //should call doBar(B::barString)
    c.doSomething();    //should call doFoo(C::fooString)
}

How would I achieve this?

  • 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-05T09:38:05+00:00Added an answer on June 5, 2026 at 9:38 am

    A possible solution:

    #include <iostream>
    #include <type_traits>
    
    class A {
    public:
        //...
        static const char fooString[];
    };
    const char A::fooString[] = "This is a Foo.";
    
    class B {
    public:
        //...
        static const char barString[];
    };
    const char B::barString[] = "This is a Bar.";
    
    class C {
    public:
        //...
        static const char fooString[];
    };
    const char C::fooString[] = "This is also a Foo.";
    
    void doFoo(const char*s) { std::cout << "doFoo: " << s << "\n"; }
    void doBar(const char*s) { std::cout << "doBar: " << s << "\n"; }
    
    template<class T>
    class Something {
    public:
        //This version should be called if T has a static member called "fooString",
        //so it should be called if T is either class A or C
        template <typename TT = T, typename std::enable_if<TT::fooString != 0, bool>::type = false>
        void doSomething() { doFoo(T::fooString); }
    
        //This version should be called if T has a static member called "barString", 
        //so it should be called if T is class B
        template <typename TT = T, typename std::enable_if<TT::barString != 0, bool>::type = false>
        void doSomething() { doBar(T::barString); }
    };
    
    
    int main()
    {
        Something<A> a;
        Something<B> b;
        Something<C> c;
    
        a.doSomething();    //should call doFoo(A::fooString)
        b.doSomething();    //should call doBar(B::barString)
        c.doSomething();    //should call doFoo(C::fooString)
    }
    

    Output:

    doFoo: This is a Foo.
    doBar: This is a Bar.
    doFoo: This is also a Foo.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to call utilities from different versions of the same
Need to call a filter function on some options based on a radio selected
I need to adapt my template to mobile versions, without creating different page files
I need to call a number of different procedures within an Oracle package successively
I have a web service which depending on the call executed I need different
I need to call a function after page load, but only once, page elements
I need to call a javascript function and pass a long url in that.
I need to call an API function after validating a form with is_valid(). This
I need to call a library function that sometimes won't terminate within a given
I need to call an external DLL that resides in the PC that hosts

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.