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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:30:14+00:00 2026-06-12T19:30:14+00:00

I need to do something like this: template <typename Matrix_xx> bool ProcessMatrix<Matrix_xx>::function1(Matrix_xx a) {

  • 0

I need to do something like this:

template <typename Matrix_xx>
bool ProcessMatrix<Matrix_xx>::function1(Matrix_xx a) {
    int x, y;

    // ... some code here ... //

    if (Matrix_xx == Matrix_1D) {
        a->readFromFile(x);
    } else if (Matrix_xx == Matrix_2D) {
        a->readFromFile(x, y);
    } // ...

}

i.e., to call different functions depends on the template argument. The code above wouldn’t compile because there are only Matrix_1D::readFromFile(int x) and Matrix_2D::readFromFile(int x, int y). I don’t want to split function1 into two different functions only because there would be a lot of doubled code. Is there another way?

  • 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-12T19:30:16+00:00Added an answer on June 12, 2026 at 7:30 pm

    Wrap the type-specific code in either overloaded function or explicitly specialized template:

    void doReadFromFile(Matrix_1D &a, int x, int y)
    {
        a->readFromFile(x);
    }
    
    void doReadFromFile(Matrix_2D &a, int x, int y)
    {
        a->readFromFile(x, y);
    }
    
    template <typename Matrix_xx>
    bool ProcessMatrix<Matrix_xx>::function1(Matrix_xx a) {
        int x, y;
    
        // ... some code here ... //
    
        doReadFromFile(a, x, y);
    }
    

    If Matrix_xx is Matrix_1D, overloading will select the first overload, if it is Matrix_2D, overloading will select the second overload and if it’s anything else, it won’t compile. But if somebody provides new type of matrix, they can make it compile by defining the doReadFromFile for it.

    This is generally useful trick and reason why standard library uses “traits”—they can be defined for class somebody gives you and they can be defined for non-class types. The “traits” can be either in the form of explicitly specialized templates or free functions, usually looked up with argument-dependent lookup (placed in the namespace of their argument, not the template).

    For completeness, the explicit specialization would look like:

    template <typename Matrix_xx>
    struct doReadFromFile {};
    
    template <>
    struct<Matrix_1D> struct doReadFromFile {
        void operator()(Matrix_1D &a, int x, int y) {
            a->readFromFile(x);
        }
    }
    
    template <>
    struct<Matrix_1D> struct doReadFromFile {
        void operator()(Matrix_1D &a, int x, int y) {
            a->readFromFile(x, y);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this: typedef int AnotherType; template <typename T> Func( T Value
I need a template like this, which work perfectly template <typename container> void mySuperTempalte
Okay, simple template question. Say I define my template class something like this: template<typename
I need to create a template function like this: template<typename T> void foo(T a)
I need something like this http://jonraasch.com/blog/a-simple-jquery-slideshow but w/o the absolute positioning. Is it possible?
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
What I need is something like this: /<[\w\d]+ ([\w\d]+\=[w\d])+\/>/ Something that would match several
I need to do something like this: <input type=button value=click id=mybtn onclick=myfunction('/myController/myAction', 'myfuncionOnOK('/myController2/myAction2', 'myParameter2');',
I need to do something like this: Section 1, Chapter 1 title is Dogs
Hello I need to do something like this: UPDATE tbl SET pozn=CONCAT(pozn, '+attach_str+') WHERE

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.