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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:43:54+00:00 2026-06-14T01:43:54+00:00

So I had an problem with partial specialization of function templates. I choose the

  • 0

So I had an problem with partial specialization of function templates. I choose the solution described here: Question

Now I have this:

#include <vector>
#include <iostream>

template <typename T> struct helper {
    static void print(T value) { std::cout << value; }
};
template <typename T> struct helper<std::vector<T>> {
    static void print(std::vector<T> const &value) { }
};

template <typename T>
void print (T const &value) {
    // Just delegate.
    helper<T>::print (value);
}


int main () {
    print (5);
    std::vector<int> v;
    print (v);
}

But I want to have this layout:

helper.hpp

#include <vector>
#include <iostream>

template <typename T> struct helper {
    static void print(T value) { std::cout << value; }
};

vector_helper.cpp

#include <vector>
#include <iostream>

template <typename T> struct helper<std::vector<T>> {
    static void print(std::vector<T> const &value) { }
};

print.hpp

#include "helper.hpp"

template <typename T>
void print (T const &value) {
    // Just delegate.
    helper<T>::print (value);
}

main.cpp

#include "print.hpp"

int main () {
    print (5);
    std::vector<int> v;
    print (v);
}

Compiled like this:

g++ main.cpp vector_helper.cpp

The problem is that MinGW is producing linking-time erros: undefined reference to helper<vector<...>>::print(vector<...>)

When I add the line:

#include "vector_helper.cpp"

before int main() {...}, it compiles fine and also works. How can I solve it, because I want to add the class specialization in the file linked by the g++ command.

  • 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-14T01:43:55+00:00Added an answer on June 14, 2026 at 1:43 am

    Those template classes can’t be split into separate object files and remain wholly unspecialised. If you look at the standard templates like vector, you’ll see that everything is in a single headerfile for this reason.

    If you want to hide the implementation of your templates like that, you have to force instantiation of them to one or more specific types. You might do this by sticking something like

    template class helper<std::vector<int>>;
    

    at the end of vector_helper.cpp, if I recall correctly. But you’re best off keeping all your templates in headers.

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

Sidebar

Related Questions

Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I had this problem before and can't for life of me remember how to
I had this problem some time ago and I gave up but lately it
Problem I have Telerik TabControl and each tab content is a partial view. Everything
I have a problem I can not resolve on a form Here's my view:
I'm currently hacking on an events site and have never had a problem optimizing
I had this problem a long time ago and recently I got some clues
I have had a problem where it appeared as though the second execution of
I had a Problem to use DotNet 4.0 DLL in a DotNet 2.0 Application,
i had a problem on confusing how to put ontouch inside my coding because

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.