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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:06:33+00:00 2026-05-17T01:06:33+00:00

I have the following piece of code: class ICookable { public: virtual void CookMe

  • 0

I have the following piece of code:

class ICookable
{
public:
    virtual void CookMe () = 0;
    virtual ~ICookable () {};
};

class Egg : public ICookable
{
public:
    virtual void CookMe () {cout << "Egg cooked!" << endl;}
};

template <class T>
void Cook (T&)
{
    cout << "Item Uncookable!" << endl;
}

template <>
void Cook (ICookable& c)
{
    c.CookMe ();
}

int _tmain(int argc, _TCHAR* argv[])
{
    Egg egg;
    Cook (egg);
    return 0;
}

I want the Cook function to behave differently depending on whether its parameter is inherited from ICookable interface or not.
However in the example above I get “Item Uncookable” message for Egg parameter, unless I manually write:

Cook<ICookable> (egg);

Is there a way to let the compiler automatically choose the right realization for ICookable descendants?

  • 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-17T01:06:34+00:00Added an answer on May 17, 2026 at 1:06 am

    Compile-time dispatch through boost::is_base_of and class template partial specialization:

    template<class T, class _>
    struct CookDetail {
      static void cook(T& obj) {
        cout << "uncookable\n";
      }
    };
    template<class T>
    struct CookDetail<T, boost::true_type> {
      static void cook(ICookable& obj) {
        obj.CookMe();
      }
    };
    
    template<class T>
    void cook(T& obj) {
      return CookDetail<T, boost::is_base_of<ICookable, T> >::cook(obj);
    }
    

    However, having cook(non_icookable_obj) “work” is an error to me. I’d prefer the compile-time error for trying to cook a non-cookable.

    Note that what you tried fails because that’s just how function resolution works. An exact match through a template (cook<Egg>(Egg&)) is considered better than one which requires a conversion (cook(ICookable&)). Details are in the FCD if you want the arcane technicalities.

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

Sidebar

Related Questions

I have the following piece of code: class Foo { public Foo() { Bar
Let's say we have the following piece of code: public class Event { }
I have the following piece of code in my DataModel.cs class: public User ValidateUser(string
If I have the following piece of Ruby code: class Blah def self.bleh @blih
I have the following piece of code in Visual C++ 2005 : : class
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have following piece of code NSMutableArray *mutArray = [[NSMutableArray alloc] init]; [mutArray addObject:
Consider the following piece of code: class B { private: // some data members
Following on from a previous question , I am having trouble combining the Lazy<T>
First, I know that it doesn't make sense to compare the dllimport attribute and

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.