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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:42:11+00:00 2026-06-12T21:42:11+00:00

How does one make sure, that the specialization of a (class) template implements all

  • 0
  1. How does one make sure, that the specialization of a (class) template implements all
    functions? (Right now only when I use mul do I get an error message.)
  2. What is the difference between the specialization to int for traits1/traits2. I thought they were both template specializations, but traits2 does not accept static and gives a linker error instead of a compiler error.

.

#include <iostream>

template<typename T>
struct traits1{
  static T add(T a, T b) { return a+b; } /* default */
  static T mul(T a, T b);                /* no default */
};

template<>
struct traits1<int> {
  static int add(int a, int b) { return a*b; }
  /* static int mul(int a, int b) missing, please warn */
};

template<typename T>
struct traits2{
  static T add(T a, T b);
  static T mul(T a, T b);
};

template<>
int traits2<int>::add(int a, int b) { return a*b; }

/* traits2<int>::mul(int a, int b) missing, please warn */

int main()
{
  std::cout << traits1<int>::add(40, 2) << "\n";
  // error: mul is not a member of traits1<int>
  //std::cout << traits1<int>::mul(40, 2) << "\n";

  std::cout << traits2<int>::add(40, 2) << "\n";
  // error: undefined reference to traits2<int>::mul(int, int)
  //std::cout << traits2<int>::mul(40, 2) << "\n";
  return 0;
}
  • 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-12T21:42:12+00:00Added an answer on June 12, 2026 at 9:42 pm

    1) Don’t specialize the whole class if all you want is different behavior for one particular function. Specialize that function alone:

    template<>
    int traits1<int>::add(int a, int b) { return a*b; }
    

    You can’t make sure a specialization implements the all template methods, because they’re unrelated.

    2) You didn’t provide a definition for traits2::mul, so of course you get the linker error – the declarations is there.

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

Sidebar

Related Questions

one of the validations I want is to make sure that the user does
I want to make sure that _content does not end with a NewLine character:
How does one make random numbers in the interval -10 to 10 in C++
does any one know how to make the slide to left effect like in
Does it make any difference in C99 when one writes const int x =
How does one wait until all of the Javascript is loaded before curling a
How does one use rm to delete a file named '--help'? When I try,
How does one write a (Intel) F90 function that converts a string into lowercase
How does one optimize if the parameter space is only integers (or is otherwise
How does one add views to a window, so that the views are resized

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.