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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:44:53+00:00 2026-05-21T07:44:53+00:00

I have been looking for a design that solves the following problem. It will

  • 0

I have been looking for a design that solves the following problem.
It will take a few words to describe.

We have four types A1, A2, B, and C.

We would like to write a function fn that takes as parameter a type P.
Using traits, P resolves within A1/A2/B/C to PA1/PA2/PB/PC.

The implementation of fn is the same for PA1 and PA2, but it is
different for PB and PC, both between them as well as from that of
PA1/PA2.

#include <cassert>

struct PA1 {};
struct PA2 {};
struct PB  {};
struct PC  {};

struct A1 { typedef PA1 P; };
struct A2 { typedef PA2 P; };
struct B  { typedef PB  P; };
struct C  { typedef PC  P; };

template<typename T> char fn(typename T::P)
{
    return 'a';
}

char fn(B::P) {   return 'b';  }
char fn(C::P) {   return 'c';  }

int main()
{
    PA1 pa1;
    PA2 pa2;
    PB  pb;
    PC  pc;
    assert( fn<A1>(pa1) == 'a' );
    assert( fn<A2>(pa2) == 'a' );

    assert( fn(pb) == 'b' );
    assert( fn(pc) == 'c' );
}

The advantage of the code above is that the implementation of fn for
PA1 and PA2 is not repeated.

But here is the snag. The function calls are not symmetric. It is

fn<A1>(pa1)

and

fn<A2>(pa2)

for A1/A2/PA1/PA2, but just fn(pb) and
fn(pc) for B/C/PB/PC.

This precludes using fn(..) within yet another (not shown) class
template.

Normally this is not an issue. The template parameter can be deduced
for parameterized functions. That doesn’t work here. We’d be asking
the compiler to locate the type among A1/A2/B/C for which P resolves
to one of PA1/PA2/PB/PC.

What would you do?

  • 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-21T07:44:53+00:00Added an answer on May 21, 2026 at 7:44 am

    I think that you are confusing two things here:

    The implementation of fn is the same for both PA1 and PA2

    Does not preclude PA1 and PA2 from being distinct types with distinct typedef.

    Which we can therefore use as is:

    template <typename PA>
    char fn(PA pa) {
      typedef typename PA::type Type; // A1 or A2
      return 'a';
    }
    
    char fn(PB);
    char fn(PC);
    

    If you cannot actually modify P itself, you can always introduce a traits class.

    template <typename T>
    struct PTraits;
    
    template <>
    struct PTraits<PA1> { typedef A1 type; };
    
    template <>
    struct PTraits<PA2> { typedef A2 type; };
    
    // ...
    

    I would note that Herb Sutter recommend not to use function specializations and to prefer overload, because the interaction between overload (esp. with template involved) is specialization is extremely tricky… and it’s hard to fathom which function will get called from a given set of parameters.

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

Sidebar

Related Questions

I have been looking into MVVM design patterns with WPF for a project. I
Have been looking at the MVC storefront and see that IQueryable is returned from
I have been looking at game engine design (specifically focused on 2d game engines,
I have been trying to find a good-looking design using Aero in Delphi 2010.
I have been looking at the decorator design pattern (I am new to the
I have been looking at a class that has a method that accepts a
I have been looking for help online on how to design my php classes
I have recently been looking at code, specifically component oriented code that uses threads
I have just been looking at the 'Forgiving Format' design pattern (e.g. http://ui-patterns.com/patterns/ForgivingFormat ),
I have an interesting problem which I've been looking into and would appreciate some

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.