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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:11:14+00:00 2026-05-30T15:11:14+00:00

This could be very silly… If I have such a template class A, template

  • 0

This could be very silly…

If I have such a template class A,

template <class T1, class T2>
struct A
{
    T1 a;
    T2 b;
};

and a function works on it,

template <class T1, class T2>
void foo(A<T1, T2> *p)
{
}

now I subclass A,

struct B : A<int, int>
{
};

B b[100];

I can not deduce template parameter…

foo(b);

and I have to write like this,

foo((B *)b);

or like this,

foo<int, int>(b);

and now something more,
I wish foo can accept something T other than A and treat them as A

template <class T>
struct A<T, void>
{
    T a;
};

template <class T>
void foo(T *p)
{
    foo((A<T, void> *)p);
}

now unless I write like this, the later foo is called…

foo<int, int>(b);
  1. How can I make deducing template parameters from array of subclass easier?
  2. How can I make subclass have higher priority than other class when locating overloaded function?

This seems will do

template <class T>
struct is_A_derived
{
    typedef char (&yes)[1];
    typedef char (&no)[2];

    template <class T1, class T2>
    static yes test(A<T1, T2> *);
    static no test(...);

    template <class T1, class T2>
    static A<T1, T2> get_type(A<T1, T2> *);
    static A<T, void> get_type(...);

    template <class T1, class T2>
    static A<T1, T2> base_type(A<T1, T2> *);
    static int base_type(...); // guess or maybe hope this "int" is never used

    static const bool value = sizeof(test((T *)0)) == sizeof(yes);
    static const bool identical = value && sizeof(base_type((T *)0)) == sizeof(T);

    typedef typename std::conditional<identical, decltype(get_type((T *)0)), A<T, void>>::type type;
};
  • 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-30T15:11:16+00:00Added an answer on May 30, 2026 at 3:11 pm

    In the first case, there is actually a danger of decaying an array of B to an A* pointer, in case you happen do to pointer arithmetic on the A* pointer.

    It surprises me that it doesn’t resolve but could be a nice safety feature, just in case you are planning to perform a function over an array of A, which won’t work with an array of B.

    You could do foo( &b[0] ) rather than casting or explicit templating.

    In the latter case you are now complicating things with a function foo on your T* if it has the same name as the function on the A template.

    Once you add the second foo, the call on B will indeed choose the second foo because it now becomes a perfect match. The subclass can be given a higher priority with clever SFINAE techniques to see if it is an A or not. Generally though I would avoid using this unless you really have to.

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

Sidebar

Related Questions

Introduction: Now I know this question could be very broad and it would be
Okay this could very likely be a silly question. I am using Entity Framework
This could be a very silly question to any long time C++, visual studio
Hihi all, This could very well be a silly question. I would like to
This could be weird, Have you ever come across a blog which you wanted
I have a very complicated issue, but if I can do this, then I
This could very well be a basic question - but I was unable to
I've about pulled my hair out with this one.. I understand this could very
Okay, this might be a very silly beginner question, but: I've got an ClassA,
I have a very simple table called genre , which looks like this: GENRE

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.