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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:33:44+00:00 2026-06-08T10:33:44+00:00

Okay, this is a bit complicated so please bear with me. :) We have

  • 0

Okay, this is a bit complicated so please bear with me. 🙂

We have this simple class hierarchy:

class A {};
class DA : public A {};
class DDA : public DA {};

And we have the following functions operating on these classes:

void f(A x) {
  std::cout << "f A" << std::endl;
}
void f(DA x) {
  std::cout << "f DA" << std::endl;
}
void f(DDA x) {
  std::cout << "f DDA" << std::endl;
}

Now we want to add another function that treats DA a little differently.

(1) A first attempt could look like this:

void g(A t) {
  std::cout << "generic treatment of A" << std::endl;
  std::cout << "called from g: ";
  f(t);
}
void g(DA t) {
  std::cout << "special treatment of DA" << std::endl;
  std::cout << "called from g: ";
  f(t);
}

But calling this with an object of each of the classes clearly does not have the desired effect.

Call:

  A a; DA b; DDA c;
  g(a); g(b); g(c)

Result:

generic treatment of A
called from g: f A
special treatment of DA
called from g: f DA
special treatment of DA
called from g: f DA        //PROBLEM: g forgot that this DA was actually a DDA

(2) So instead we might try to use templates:

template<typename T>
void h(T t) {
  std::cout << "generic treatment of A" << std::endl;
  std::cout << "called from h: ";
  f(t);
}

template<>
void h<>(DA t) {
  std::cout << "special treatment of DA" << std::endl;
  std::cout << "called from h: ";
  f(t);
}

which results in:

generic treatment of A
called from h: f A
special treatment of DA
called from h: f DA
generic treatment of A    //PROBLEM: template specialization is not used
called from h: f DDA

Well, how about we don’t use template specialization but define a non-template function for the special case? (Article on the very confusing matter.) It turns out it behaves in exactly the same way because the non-template function which is according to the article a “first class citizen”, seems to lose because a type conversion is necessary to use it. And if it would be used, well then we would just be back at the first solution (I assume) and it would forget the type of DDA.

(3) Now I came across this code at work which seems rather fancy to me:

template<typename T>
void i(T t, void* magic) {
  std::cout << "generic treatment of A" << std::endl;
  std::cout << "called from i: ";
  f(t);
}

template<typename T>
void i(T t, DA* magic) {
  std::cout << "special treatment of DA" << std::endl;
  std::cout << "called from i: ";
  f(t);
}

But it seems to do exactly what I want:

generic treatment of A
called from i: f A
special treatment of DA
called from i: f DA
special treatment of DA
called from i: f DDA

Even though it needs to be called in a weird way:
i(a, &a); i(b, &b); i(c, &c);

Now I have several questions:

  1. Why the hell does this work?
  2. Do you think it is a good idea? Where are possible pitfalls?
  3. Which other ways of doing this kind of specialization would you suggest?
  4. (How do type conversions fit into the madness that is template partial ordering and such…)

I hope this was reasonably clear. 🙂

  • 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-08T10:33:47+00:00Added an answer on June 8, 2026 at 10:33 am

    The function template overload

    template<typename T>
    void i(T t, DA* magic) {
    

    will only be available if the parameter magic is convertible to type DA *. This is obviously the case for &b but also for &c as pointer-to-derived is convertible to pointer-to-base. The void * function template overload is always available but DA * is preferred to void *, per §13.3.3.2:4:

    c++11

    13.3.3.2 Ranking implicit conversion sequences [over.ics.rank]

    […]

    4 Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a
    Promotion, which is a better conversion than a Conversion. Two conversion sequences with the same rank
    are indistinguishable unless one of the following rules applies:

    […]

    — If class B is derived directly or indirectly from class A, conversion of B* to A* is better than conversion
    of B* to void*, and conversion of A* to void* is better than conversion of B* to void*.

    As you’ve noted, it’s a perfectly workable scheme; it would make more sense to wrap the magic in another template function that takes care of calling i with (a, &a):

    template<typename T>
    void j(T t) {
        i(t, &t);
    }
    

    In terms of safety, it’s fine; if the DA * overload is lost then the void * overload will be selected silently; it’s up to you to decide whether this is desirable.

    As an alternative, you can use std::enable_if to select between templates:

    template<typename T>
    typename std::enable_if<!std::is_base_of<DA, T>::value>::type g(T t) {
      std::cout << "generic treatment of A" << std::endl;
      f(t);
    }
    template<typename T>
    typename std::enable_if<std::is_base_of<DA, T>::value>::type g(T t) {
      std::cout << "special treatment of DA" << std::endl;
      f(t);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so this is a bit confusing (well to me). I have a string
Okay, I have done a bit of searching online and found this thread, but
Okay this question is very simple: I have a facebook page, and a website.
Okay this may be a simple question but I have yet to come with
Okay, this is a bit messy: I'm using Netbeans, and I have a main
Okay this is going to seem really dumb but bear with me please. A
Okay this may sound a bit weird but it is what I have to
Okay, here's my problem. Please forgive me as it's a little bit complicated. I'm
Okay, this problem is a bit odd. I have an application built in VS2010
I have a bit of problem with this. I have a class A which

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.