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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:13:19+00:00 2026-06-08T04:13:19+00:00

I have the following problem: template<class T> void set(std::string path, const T data) {

  • 0

I have the following problem:

template<class T>
void set(std::string path, const T data)
{
   stringstream ss;
   ss << data << std::endl;
   write(path, ss.str();
}

template<class T>
void set(std::string path, const T data)
{
    std::stringstream ss;
    for(typename T::const_iterator it = data.begin(); it < data.end(); ++it)
    {
       ss << *it;
       if(it < data.end() -1 )
          ss << ", ";
    }
    ss << std::endl;
    write(path, ss.str());
}

I get the following error:

error: ‘template<class T> void myclass::set(std::string, T)’ cannot be overloaded
error: with ‘template<class T> void myclass::set(std::string, T)’

Is there a way to differentiate between container types and other types in templates?

  • 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-08T04:13:20+00:00Added an answer on June 8, 2026 at 4:13 am

    In C++03 you can do this with a little bit of SFINAE to selectively enable different versions of the function for different types:

    #include <boost/type_traits.hpp>
    #include <sstream>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template<class T>
    void set(typename boost::enable_if<boost::is_pod<T>, std::string>::type path, const T data)
    {
       std::cout << "POD" << std::endl;
       stringstream ss;
       ss << data << std::endl;
    }
    
    template<class T>
    void set(typename boost::disable_if<boost::is_pod<T>, std::string>::type path, const T data)
    {
        std::cout << "Non-POD" << std::endl;
        std::stringstream ss;
        for(typename T::const_iterator it = data.begin(); it < data.end(); ++it)
        {
           ss << *it;
           if(it < data.end() -1 )
              ss << ", ";
        }
        ss << std::endl;
    }
    
    int main() {
      int i;
      float f;
      std::vector<int> v;
      set("", v);
      set("", i);
      set("", f);
    }
    

    I used boost for convenience here, but you could roll your own if boost isn’t an option or use C++11 instead.

    is_pod isn’t quite what you want really, you probably want an is_container trait, but that’s not so trivial, you’ll need to make a trait of your own and is_pod makes a good approximation for how to use traits to selectively enable functions as simple answer.

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

Sidebar

Related Questions

Say I have the following template function: template <class T> void apply(const vector<complex<T> >&
I have a problem with the following code: template <typename U> class lamePtr {
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem with inheritance and templates: class Base {}; class Deriv
I have a problem with nested templates and their template specialization. Given the following
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
Hi i have following Problem. I write a Mediawiki Extension where i need some
I have the following problem, from c++ I send huge string[] to java. huge
I have a problem with giving a template class a template friend in Sun
I have the following the class: //Rectangle.h #include Point.h class Rectangle{ public: Rectangle(); void

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.