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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:07:53+00:00 2026-05-25T11:07:53+00:00

Normally when you write a template class and have a specialization of a function

  • 0

Normally when you write a template class and have a specialization of a function in that class, you need to put the specialization in the .cpp file since it is a concrete definition (rather than a template definition). If you only have one tiny function to specialize though this is a little annoying and sometimes you may forget to look in that .cpp file for that one last definition which can cause confusion.

In a situation like this it would be nice to include that one specialization in the header file with the rest of the template definition. Is there any trick that would allow this to be achieved?

  • 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-25T11:07:53+00:00Added an answer on May 25, 2026 at 11:07 am

    Use the inline keyword:

    template <unsigned N>
    unsigned get_const()
    {
        return N;
    }
    
    template <>
    inline unsigned get_const<42>()
    {
        throw "meaning of life";
    }
    

    The keyword indicates that multiple definitions should assumed to be the same, and therefore not an error. (This is orthogonal to static, which keeps repeat definitions in their own translation unit.)

    Note that this works with all functions, not just templates or specializations.

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

Sidebar

Related Questions

I have a template statistics class that has range parameters. template <typename T> class
I am new to OOP in PHP (normally write software). I have read that
If I have a button: <button id=button1> Normally I would write: $(#button1).click(function () {
I'm normally a Linux guy, but I need to write a batch script on
We all know that Android runs Dalvik VM programs. Normally developers write programs in
I have to write an assembly program that will find the minimum of array
Normally you write a handler for a button click like this: $(document).ready(function() { $(button).click(function()
I have a InfoPath template that submits it's forms to a SharePoint server. If
I'm inserting many dates into a database. Most of them write normally, so that
I’m dong some SQL work this week and I normally write Java. I need

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.