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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:44:40+00:00 2026-06-13T23:44:40+00:00

Possible Duplicate: partial specialization of function template I can’t find anywhere a solution for

  • 0

Possible Duplicate:
partial specialization of function template

I can’t find anywhere a solution for my problem, because if I search with the keywords I come up with would give me solutions suited for different problems. I understand that this must been asked before, just can’t find a solution.

Suppose I have a function template:

template<class any> print(any value);

I can specialize it like this for let’s say a int:

template<> print<int>(int value)
{
    std::cout << value;
}

But now the problem, I want it to work with a vector as well. Since the vector class is a template class it becomes difficult.

Specializing the function like this:

template<class any> print<vector<any> >(vector<any> value) {}

Will generate the following error (MinGW g++):

FILE: error: function template partial specialization 'print<vector<any> >' is not allowed

Note that the function print is just an example.

How can I solve this?

  • 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-13T23:44:41+00:00Added an answer on June 13, 2026 at 11:44 pm

    There is a general workaround in which the function-template just delegates the job to class template member functions:

    #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);
    }
    

    However, if you can come by with simple function overloading (as suggested by ecatmur and Vaughn Cato), do so.

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

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: “invalid use of incomplete type” error with partial template specialization Why is
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: MySQL full text search with partial words I have a search feature
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: C and C++ : Partial initialization of automatic structure While reading Code
Possible Duplicate: MySQL query that computes partial sums ok, the question is not clear
Possible Duplicate: What are the stages of compilation of a C++ program? I find
Possible Duplicate: Generic partial view: how to set a generic class as model? I

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.