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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:43:26+00:00 2026-05-27T16:43:26+00:00

Using VC++ 2010, given the following: class Base { }; class Derived : public

  • 0

Using VC++ 2010, given the following:

class Base { };
class Derived : public Base { };

template<class T> void foo(T& t);  // A
void foo(Base& base);              // B

Derived d;
foo(d);                            // calls A
foo(static_cast<Base&>(d));        // calls B

I would like “B” to be called above. I can achieve this with a cast to Base, but why is this necessary?

I want the template function to be called for all types not derived from Base (built-in types, etc.), but I want the non-template overload to be called for types derived from Base, without requiring the client to explicitly cast. I also tried making the overload a specialization of the template, but the same behavior occurs in that case. What is the idiomatic way to get what I’m looking for?

  • 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-27T16:43:27+00:00Added an answer on May 27, 2026 at 4:43 pm

    All things being equal, nontemplate functions are preferred over function templates. However, in your scenario, all things are not equal: (A) is an exact match with T = Derived, but (B) requires a derived-to-base conversion of the argument.

    You can work around this for specific cases (like this one) by using SFINAE (substitution failure is not an error) to prevent (A) from being instantiated with a type that is derived from Base:

    #include <type_traits>
    #include <utility>
    
    template <typename T>
    typename std::enable_if<
        !std::is_base_of<Base, T>::value
    >::type foo(T& x)
    {
    }
    
    void foo(Base& x)
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Visual Studio 2010. I have a class with the following constructor: CVideoAnnotation::CVideoAnnotation(std::string
I have the following question: Assume base class A with method: A& operator+(A& a)
I have the following: #include<iostream> #include<unordered_map> #include<tuple> using namespace std; class CTest { //
I am trying to implement a leftist tree using heaps as a base class.
Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program {
I have an issue when sorting dates using jqGrid . Given the following dates
I have this... template <typename Key, typename Value> class A { public: //... private:
I have three tables... Given the following tables I would like to know how
I've created a MVC website using Visual Studio 2010 and would like to deploy
We have begun using the following branching structure in TFS 2010: All changes so

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.