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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:17:15+00:00 2026-06-10T22:17:15+00:00

I have a template class, say: template<class T> class someClient { void someCallbackA() {foo_->onA();}

  • 0

I have a template class, say:

template<class T>
class someClient
{
void someCallbackA() {foo_->onA();}
void someCallbackB() {foo_->onB();}

private:
T* foo_;
};

which I can instantiate with a bunch of different types T which support the onA and onB interface. I happen to have a case where two out of the several different types T I use needs a particular behavior controlled from someClient so I need to add some function doBar() to these two types (call them Edge1 and Edge2). Then I want a part of the someClient code to call foo_->doBar() but without breaking when the type of foo_ does not have that. Is there a way to use boost::enable_if to have a someClient::doBar() which will call foo_->doBar() only for those two types, but not be there, or expand to nothing if the types are not Edge1 or Edge2?

I was thinking along the lines of:

template <class T, enable_if<mpl_or<is_same<T,Edge1>, is_same<T,Edge2> > >
someClient<T>::doBar() {foo_->doBar();}
  • 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-10T22:17:16+00:00Added an answer on June 10, 2026 at 10:17 pm

    I think this does what you want. I used C++11 <type_traits> instead of boost’s:

    struct Edge {
        void doBar() { std::cout << "did Bar."; }
    };
    
    template<typename T>
    class someClient
    {
    public:
    
        template<typename U = T>
        typename
        std::enable_if<std::is_same<U, Edge>::value, void>::type
        doBar() { foo_->doBar(); }
    
        template<typename U = T>
        void doBar( typename std::enable_if<!std::is_same<U, Edge>::value, void>::type* = 0 )
        { /* do nothing */ }
    
    
    private:
        T* foo_;
    };
    
    int main()
    {
        someClient<int> i;
        someClient<Edge> e;
        i.doBar();
        e.doBar();  // outputs "did Bar."
    }
    

    doBar() needs to be template itself for this to work, explanation here: std::enable_if to conditionally compile a member function

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

Sidebar

Related Questions

Let's say we have a template class Area , which has a member variable
Say I have a templated Action template <class ArgT> struct Action { Action(::boost::function< void(ArgT)
Say I have this: template<typename T, int X> class foo { public: void set(const
Say I have a templated class: template <typename T> class foo { void do_someting(T
Let's say I have a template: template <class N, class I> void add(N* element,
Let's say you have a c++0x std::array member of a template class and you
Lets say you have simple template function (not class member for the sake of
Say I have a templated class like template <typename T> struct Node { //
Let's say I have a simple Server with a template which accepts a Client
Say I have a class that can use different types of distance functions (Euclidean

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.