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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:11:15+00:00 2026-06-14T12:11:15+00:00

I have been looking all over the internet and stackoverflow for a concrete answer

  • 0

I have been looking all over the internet and stackoverflow for a concrete answer but I can’t seem to find one. I have to create a generic class and then implement specific functions. My specific instructions were: You need to make use of Template Expression Parameters and Template Class Specialization and Partial Specialization.

I have a template class:

template <class T, int x, int y>
class Z {
    T **array[x][y];
    public:
         Z();
         void print();
         //and other methods
};

I need to:

1) Only Z’s where x= 2 and y = 2 need to have a public method void J()

2) For char Z’s of x = 2 and y= 2 J will do something; for everything else it does something else

3) For only Z’s where T is char will the array be initialized to some value. For everything else it’s 0

Naturally, this works:

template<class T, int x, int y>
Z<T,x,y>::Z<T,x,y>() { //initialize to 0 } 

But this doesn’t:

template<int x, int y>
Z<char,x,y>::Z<char,x,y>() { //initialize to something}

And likewise (assume J exists) this does not work:

template <class T>
void Z<T,2,2>::J() { //something }

My question is:

Is there any simple method for implementing the above items? I need to keep all the other methods in Z. Giving a hint or pointing in the right direction (maybe I missed a question since there are a lot) would be helpful.

Thanks.

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

    It seems you want to define only some functions of some specializations : if print() does not change between the char specialization and the general case, it seems that you don’t want to redefine it.

    // What you want to do (illegal in C++)
    template<int,typename T>
    struct Z
    {
        T myValue;
        Z();
        void print() { /* ... */ }
    };
    
    template<int i, typename T>
    Z<i,T>::Z() { /* ... */ }
    
    template<int i>
    Z<i,char>::Z() { /* ... */ }
    

    However, it does not work like this. Partial or full specializations of class have almost nothing in common, except ‘prototype’ of template parameters:

    // The two following types have only two things related: the template parameter is an int,
    // and the second type is a full specialization of the first. There are no relations between
    // the content of these 2 types.
    template<int> struct A {};
    template<> struct A<42> { void work(); };
    

    You have to declare and define each (partial) specialization:

    // Fixed example
    template<int,typename T>
    struct Z
    {
        T myValue;
        Z();
        void print() { /* ... */ }
    };
    template<int i, typename T>
    Z<i,T>::Z() { /* ... */ }
    
    // Specialization for <all-ints,char>
    template<int i>
    struct Z<i,char>
    {
        char myValue;
        char othervalue;
        Z();
        void print() { /* Same code than for the general case */ }
    };
    
    template<int i>
    Z<i,char>::Z() { /* ... */ }
    

    The only way to escape the code duplication is by using inheritance of traits:

    // Example with the print function
    template<typename T>
    struct print_helper
    {
        void print() { /* ... */ }
    };
    
    // Fixed example
    template<int,typename T>
    struct Z : public print_helper<T>
    {
        T myValue;
        Z();
    };
    template<int i, typename T>
    Z<i,T>::Z() { /* ... */ }
    
    // Specialization for <all-ints,char>
    template<int i>
    struct Z<i,char> : public print_helper<char>
    {
        char myValue;
        char othervalue;
        Z();
    };
    
    template<int i>
    Z<i,char>::Z() { /* ... */ }
    

    You cannot do what you want without duplication for the moment (the feature removing code duplication is static if and has been proposed for the next C++ standard, see n3322 and n3329).

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

Sidebar

Related Questions

I have been looking all over for an answer to this question, but can't
I have been looking all over the Internet for an answer to this question
Hi i've been looking all over and can't find the answer to this. I
I've been looking all over the internet and I can't find an acceptable solution
I've been looking all over the internet for an answer, and I didn't find
I have been looking all over MSDN and can't find a reason why Thread
I have been looking all over the internet and i found some good guides
I've been looking all over the place, but I haven't landed an answer yet.
Right guys, I have been looking all over the internet for a tutorial to
I have been looking all over google to find some answers to my questions

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.