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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:07:27+00:00 2026-05-26T06:07:27+00:00

Please consider the following example: #include <string> #include <vector> using std::string; using std::vector; template

  • 0

Please consider the following example:

#include <string>
#include <vector>

using std::string;
using std::vector;

template <typename T>
const T GetValue()
{
    return T(); // some value
}

template <typename T>
const vector<T> GetValue()
{
    return vector<T>(); // some vector of values
}

int main(int argc, char* argv[])
{
    int i = GetValue<int>();
    vector<int> = GetValue<vector<int>>();
    return 0;
}

I have two template functions which are supposed to parse values from some storage depending on an given type. The first should do the job for simple data types, the second for vectors of simple data types only.
My problem is that the template matching is ambiguous since T may be vector<T>.
I wonder how to implement the overload/specialization for the vector types properly.

Any help would be greatly appreciated!

  • 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-26T06:07:27+00:00Added an answer on May 26, 2026 at 6:07 am

    One simple way is to use an out-param, so that the template parameter can be deduced from the argument:

    #include <vector>
    using std::vector;
    
    template <typename T>
    void GetValue(T &t)
    {
        t = T(); // some value
    }
    
    template <typename T>
    void GetValue(vector<T> &v)
    {
        v = vector<T>(); // some vector of values
    }
    
    int main(int argc, char* argv[])
    {
        int i;
        GetValue(i);
        vector<int> v;
        GetValue(v);
        return 0;
    }
    

    GetValue(v) isn’t ambiguous, since the template argument deduction rules say that the second overload is the better match.

    This isn’t necessarily the interface/style you want, though, in which case you could use partial specialization instead of overloading. But that requires a class, since function templates cannot be partially specialized:

    #include <vector>
    using std::vector;
    
    template <typename T>
    struct Getter {
        T get(void) {
            return T(); // some value
        }
    };
    
    template <typename T>
    struct Getter<vector<T> > {
        vector<T> get(void) {
            return vector<T>(); // some vector of values
        }
    };
    
    template <typename T>
    T GetValue(void)
    {
        return Getter<T>().get();
    }
    
    int main(int argc, char* argv[])
    {
        int i = GetValue<int>();
        vector<int> v = GetValue<vector<int> >();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following simplified example: #include boost/shared_ptr.hpp #include boost/smart_ptr/enable_shared_from_this.hpp using namespace boost; class
please consider following code #include <iostream> using namespace std; class Digit { private: int
please consider the following code: template <typename T> struct foo { template <typename S>
Please consider the following: <td style=width: 500px;> <div style=width: 400px;>SomeContent</div> </td> For some reason,
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<
Please consider the following code and the explanation from this Mozilla tutorial Using web
Please consider the following HTML: <td> Some Text <table>.....</table> </td> I need to manipulate
consider the following example: public IEnumerable<String> Test () { IEnumerable<String> lexicalStrings = new List<String>
Consider the following code template<typename T, int N> struct A { typedef T value_type;

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.