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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:06:13+00:00 2026-05-22T22:06:13+00:00

Imagine the following scenario: template<class T> void myFunction(T *) { //do nothing } void

  • 0

Imagine the following scenario:

    template<class T>
    void myFunction(T *)
    {
       //do nothing
    }

    void myFunction(myBase * _base)
    {
       //do something with _base
    }

int main( int argc, const char* argv[] )
{
    myDerivedFromBase * ptr = new myDerivedFromBase;
    myFunction(ptr); //calls the templated version

    myFunction(static_cast<myBase*>(ptr)); //calls the correct version

    delete ptr;
}

basically I want to achieve that the templated function gets called for pointers, that are not derived from my base. If a ptr is derived from myBase I want that the second version of myFunction gets called without the explicit cast. Is that possible?

  • 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-22T22:06:14+00:00Added an answer on May 22, 2026 at 10:06 pm

    Use type traits to prevent the template from binding:

    template<typename T>
    typename std::enable_if<!std::is_base_of<myBase, T>::value, void>::type myFunction(T*)
    {
    }
    

    If you can’t use C++0x, use Boost’s type traits library instead.

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

Sidebar

Related Questions

Imagine following scenario: We have a lot of parallel development going on in several
Imagine having the following scenario: You need to create a system where the Back
Imagine the following tables: create table boxes( id int, name text, ...); create table
Imagine the following scenario: some xml file is downloaded from server and stored on
Imagine the following scenario: I am using SQL Server 2005. I have a transaction
Imagine the following scenario: You are asked to develop a tool from scracth, by
Imagine the following two entities. Element is simple class containing some data: @Entity public
Imagine the following class that displays some sort of hierarchy: class BaseList2D(object): def __init__(self):
Imagine the following scenario: you have a set of RPG character spritesheets in PNG
Imagine the following scenario. I have a checkbox that the user can check to

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.