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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:46:23+00:00 2026-05-15T13:46:23+00:00

I have a method foo in class C which either calls foo_1 or foo_2.

  • 0

I have a method foo in class C which either calls foo_1 or foo_2.
This method foo() has to be defined in C because foo() is pure virtual in BaseClass and I actually
have to make objects of type C. Code below:

template <class T>
class C:public BaseClass{

  void foo() {
    if (something()) foo_1;
    else foo_2;

  }
  void foo_1() {
  ....
  }

  void foo_2() {
    ....
     T t;
     t.bar(); // requires class T to provide a method bar() 
     ....
   }
};

Now for most types T foo_1 will suffice but for some types foo_2 will be called
(depending on something()). However the compiler insists on instantiating both foo_1
and foo_2 because either may be called.

This places a burden on T that it has to provide
a bar method.

How do I tell the compiler the following:

  • if T does not have bar(), still allow it as an instantiating type?
  • 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-15T13:46:24+00:00Added an answer on May 15, 2026 at 1:46 pm

    you could use boost.enable_if. something like this:

    #include <boost/utility/enable_if.hpp>
    #include <iostream>
    
    struct T1 {
        static const bool has_bar = true;
        void bar() { std::cout << "bar" << std::endl; }
    };
    
    struct T2 {
        static const bool has_bar = false;
    };
    
    struct BaseClass {};
    
    template <class T>
    class C: public BaseClass {
    public:
        void foo() {
            do_foo<T>();
        }
    
        void foo_1() {
            // ....
        }
    
        template <class U>
        void foo_2(typename boost::enable_if_c<U::has_bar>::type* = 0) {
            // ....
            T t;
            t.bar(); // requires class T to provide a method bar() 
            // ....
        }
    
    private:
    
        bool something() const { return false; }
    
    
        template <class U>
        void do_foo(typename boost::enable_if_c<U::has_bar>::type* = 0) {
            if (something()) foo_1();
            else foo_2<U>();
        }
    
        template <class U>
        void do_foo(typename boost::disable_if_c<U::has_bar>::type* = 0) {
            if (something()) foo_1();
            // I dunno what you want to happen if there is no T::bar()
        }
    };
    
    int main() {
        C<T1> c;
        c.foo();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a constructor method similar to this: public class Foo { public Foo
I have a class Foo which has several methods like button_0_0 , button_0_1 ,
I have a class 'Collection', which has an add method. The add method should
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
I have base class Foo with method spam and class Bar which overrides spam
I have a model with an __init__ method: class Foo(models.Model): name = models.CharField(max_length=50) def
I have a method in a class as follows... class foo{ int bar::randomNum10to50(){ srand
If I have for example a class with instance method and variables class Foo
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
I have a method which I would like to invoke asynchronously: void Foo() {

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.