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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:45:23+00:00 2026-06-09T04:45:23+00:00

The problem with template specializations is that they’re treated like normal functions because there

  • 0

The problem with template specializations is that they’re treated like normal functions because there is no template parameter used anywhere anymore.

Therefor if the following code is put in a header file it works at first.

template <typename foo>
void f(foo p)
{
  std::cout << "f one" << std::endl;
}

template <>
void f<int>(int p)
{
  std::cout << "f two" << std::endl;
}

But this stops working if the header is included in two files.
In this case the error I get (with VS2010) is:

templateordering.obj : error LNK2005: "void __cdecl f<int>(int)" (??$f@H@@YAXH@Z) already defined in othertu.obj

And this is fixable by using the inline keyword as mentioned in many other questions.

template <>
inline void f<int>(int p)
{
  std::cout << "f two" << std::endl;
}

Now this raises two questions for me:

  1. Is there any other way to do this? Putting the specialized function in the source file doesn’t seem to work. Probably because I would need some sort of declaration in the header.
  2. What does inline actually do? It seems to be a common rule of thumb all over the internet that inline shouldn’t be used because the compiler “will probably inline the function the way he likes in any case”. So if the compiler may not inline a function that I declare as “inline”, why does this work?
  • 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-09T04:45:25+00:00Added an answer on June 9, 2026 at 4:45 am

    Is there any other way to do this? Putting the specialized function in the source file doesn’t seem to work. Probably because I would need some sort of declaration in the header.

    You need to declare the specialisation in the header, just like any other function. It’s up to you whether to define it inline in the header, or in (exactly one) source file; again, just like any other function. As you say, it must be declared inline if you do define it in the header.

    What does inline actually do?

    Normally, the one-definition rule requires functions to be defined in exactly one translation unit in the program. In practice this means that you can’t define a function in a header, since a header is intended to be included in more than one translation unit.

    However, sometimes you want, or need, to define functions in headers – for example, some compilers are only able to inline a function call if they can see the definition. The inline keyword relaxes the rule, so that you can define the function in multiple translation units, as long as all the definitions are identical.

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

Sidebar

Related Questions

I am creating a Multi project Template The problem is that when I run
i have bought a template that have built in contact form problem is that
I'm having a problem with one of my functions within a template class. template
I've been trying to implement a function that needs partial template specializations and fallen
I have a template class that looks something like this: template<class T> class C
I like the things that templates methods can do for me, since they can
Here is a minimal code that shows the problem: template<typename To, typename From> To
I'm messing around with template specialization and I ran into a problem with trying
I have a problem with nested templates and their template specialization. Given the following
I have a problem with a template and pointers ( I think ). Below

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.