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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:56:26+00:00 2026-06-09T12:56:26+00:00

Is the compiler unable, at compile-time, to take a pointer to a derived class

  • 0

Is the compiler unable, at compile-time, to take a pointer to a derived class and know that it has a base class? It seems like it can’t, based on the following test. See my comment at the end for where the issue occurs.

How can I get this to work?

std::string nonSpecStr = "non specialized func";
std::string const specStr = "specialized func";
std::string const nonTemplateStr = "non template func";

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


template <typename T> std::string func(T * i_obj)
{ return nonSpecStr; }

template <> std::string func<Base>(Base * i_obj)
{ return specStr; }

std::string func(Base * i_obj)
{ return nonTemplateStr; }

class TemplateFunctionResolutionTest
{
public:
    void run()
    {
        // Function resolution order
        // 1. non-template functions
        // 2. specialized template functions
        // 3. template functions
        Base * base = new Base;
        assert(nonTemplateStr == func(base));

        Base * derived = new Derived;
        assert(nonTemplateStr == func(derived));

        OtherClass * otherClass = new OtherClass;
        assert(nonSpecStr == func(otherClass));


        // Why doesn't this resolve to the non-template function?
        Derived * derivedD = new Derived;
        assert(nonSpecStr == func(derivedD));
    }
};
  • 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-09T12:56:26+00:00Added an answer on June 9, 2026 at 12:56 pm
    Derived * derivedD = new Derived;
    assert(nonSpecStr == func(derivedD));
    

    This doesn’t resolve to the non-template function as you expect it to because to do that a cast from Derived * to Base * must be performed; but this cast is not needed for the template version, which results in the latter being a better match during overload resolution.

    To force the template function to not match both Base and Derived you can use SFINAE to reject both those types.

    #include <string>
    #include <iostream>
    #include <type_traits>
    #include <memory>
    
    class Base {};
    class Derived : public Base {};
    class OtherClass {};
    
    template <typename T> 
    typename std::enable_if<
        !std::is_base_of<Base,T>::value,std::string
      >::type
      func(T *)
    { return "template function"; }
    
    std::string func(Base *)
    { return "non template function"; }
    
    int main()
    {
      std::unique_ptr<Base> p1( new Base );
      std::cout << func(p1.get()) << std::endl;
    
      std::unique_ptr<Derived> p2( new Derived );
      std::cout << func(p2.get()) << std::endl;
    
      std::unique_ptr<Base> p3( new Derived );
      std::cout << func(p3.get()) << std::endl;
    
      std::unique_ptr<OtherClass> p4( new OtherClass );
      std::cout << func(p4.get()) << std::endl;
    }
    

    Output:

    non template function
    non template function
    non template function
    template function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Unable to compile class for JSP Generated servlet error: @DefaultMessage(Last Updated: {0,date,medium} {0,time, HH:mm:ss
I'm trying to compile a class Tmp that implements an interface TmpInter . /*
We have a restriction that a class cannot act as a base-class for more
I cannot compile python in pydev in eclipse. I get the following error: unable
When I compile my application , I get following compile error. Unable to find
I'm trying to compile a Python binding, but I'm unable to find the python.h
My compiler expands it to 199711L. What does that mean? I read that __cplusplus
The compiler doesn't know where stat.h is? Error: c:\Projects\ADC_HCI\mongoose.c(745) : error C2079: 'st' uses
I developed a generic Unsigned class, or really a class template Unsigned<size_t N> that
I would like to define a templated class CTest<T> in which one of the

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.