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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:28:43+00:00 2026-05-28T17:28:43+00:00

There two unrelated structures A and B template <typename T> struct A {}; template

  • 0

There two unrelated structures A and B

template <typename T>
struct A {};

template <typename T>
struct B {};

one enum type

typedef enum  { ma, mb} M;

and class C containing function templates

class C
{
public: 
    template <typename T>
    static void f1 ( A <T> &a) {}

    template <typename T>
    static void f2 ( B <T> &b) {}

    template <typename U>
    static void algo (U &u, M m)
    {
        /*Long algorithm here                     
        ....
        */
        if ( m == ma) f1(u);
        else f2(u);
    }
};

Static method algo contains some algorithm, that is quite difficult… It modified some values and results into structure A or B.

I would like to run static method algo with objects A or B depending on M value. But how to say it to my compiler 🙂

int main()
{
A <double> a;
C::algo (a, ma); //Error

}

Error   1   error C2784: 'void C::f1(A<T>)' : could not deduce template argument for 'A<T>' from 'B<T>

A] I was thinking about pointer to function, but they are not usable with function templates.

B] Maybe a compile polymorphism could help

template <typename U, M m>
static void algo (U &u, M <m> ) { ...}  //Common for ma

template <typename U, M m>
static void algo (U &u, M <mb> ) { ...} //Spec. for mb

But this solution has one big problem: Both implementations should unnecessarily include almost the same code (why to write the algorithm twice?).

So I need one function algo() processing both types of arguments A and B. Is there any more comfortable solution?

  • 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-28T17:28:44+00:00Added an answer on May 28, 2026 at 5:28 pm

    It seems that you are using the enum to convey type information from the user. I would suggest that you don’t.

    In the simplest case if f1 and f2 are renamed f, then you can remove the if altogether and just call it. The compiler will call the appropriate overload for you.

    If you cannot or don’t want to rename the function templates, then you can write a helper template that will dispatch for you (basic class template undefined, specialisations for A and B that dispatch to the appropriate static function)

    If the enum is used for something else (that the compiler cannot resolve for you), you can still pass it around and rewrite the helper to dispatch on the enum rather than the type of the argument and you will have to rewrite the code to have the enum value as a compile time constant (simplest: pass it as template argument to algo). In this case ou can write function specialisations instead of classes if you want, as they would be full specialisations. But note that if you can avoid having to pass it you will remove a whole family of errors: passing the wrong enum value.

    // Remove the enum and rename the functions to be overloads:
    //
    struct C {  // If everything is static, you might want to consider using a
                // namespace rather than a class to bind the functions together...
                // it will make life easier
    
       template <typename T>
       static void f( A<T> & ) { /* implement A version */ }
    
       template <typename T>
       static void f( B<T> & ) { /* implement B version */ }
    
       template <typename T> // This T is either A<U> or B<U> for a given type U
       static void algo( T & arg ) {
          // common code
          f( arg ); // compiler will pick up the appropriate template from above
       } 
    };
    

    For the other alternatives, it is easier if the enclosing scope is a namespace, but the idea would be the same (just might need to fight the syntax a bit harder:

    template <typename T>
    struct dispatcher;
    
    template <typename T>
    struct dispatcher< A<T> > {
       static void f( A<T>& arg ) {
          C::f1( arg );
       }
    };
    template <typename T>
    struct dispatcher< B<T> > {
       static void f( B<T>& arg ) {
          C::f2( arg );
       }
    };
    
    template <typename T>
    void C::algo( T & arg ) {
       // common code
       dispatcher<T>::f( arg );
    }
    

    Again, getting this to work with a class might be a bit trickier as it will probably need a couple of forward declarations, and I don’t have a compiler at hand, but the sketch should lead you in the right direction.

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

Sidebar

Related Questions

For two unrelated classes class A and class B and a function B convert(const
There are two databases in SQL Server 2005: One called A and another one
There are two pictureboxes with two different images. If I click on one picture
I have two unrelated (not sharing any ancestor check in) Git repositories, one is
I'd like to make one list from two separate lists of unique items. There
I've recently created these two (unrelated) methods to replace lots of boiler-plate code in
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There are two popular naming conventions: vc90/win64/debug/foo.dll foo-vc90-win64-debug.dll Please discuss the problems/benefits associated with
There are two scenarios I need to clarify: An executable compiled with .NET 3.5

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.