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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:54:41+00:00 2026-06-02T20:54:41+00:00

I need to make a class with a generic data member, that works something

  • 0

I need to make a class with a generic data member, that works something like istream. For all types it should be able to store data that it recieves by the << operator. I want it to work for single variables unless the class is created for char, where it should work for C strings.

template <typename T>
class foo<C>{
    T* data;
public:
    void operator << (T t);
};

template <typename T>
class foo<char>: public foo<T>{
public:
    void operator << (char* str)
};

This is how I tried to solve it, but I get an erro saying: explicit specialization is using partial specialization syntax.

  • 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-02T20:54:42+00:00Added an answer on June 2, 2026 at 8:54 pm

    Is this what you intended?

    template <typename T>
    class foo
    {
        T* data;
    public:
        void operator<<(T t);
    };
    
    template <>
    class foo<char>
    {
    public:
        void operator<<(char* str);
    };
    

    If you meant to inherit behavior, you’ll need something like this:

    template <typename T>
    class foo_impl
    {
        T* data;
    public:
        void operator<<(T t);
    };
    
    template <typename T>
    class foo : public foo_impl<T>
    {
    };
    
    template <>
    class foo<char> : public foo_impl<char>
    {
    public:
        void operator<<(char* str);
    };
    

    (or, even simpler by eliminating unneeded occurrences of public)

    template <typename T>
    class foo_impl
    {
        T* data;
    public:
        void operator<<(T t);
    };
    
    template <typename T>
    struct foo : foo_impl<T>
    {
    };
    
    template <>
    struct foo<char> : foo_impl<char>
    {
        void operator<<(char* str);
    };
    

    And I would suggest some const-correctness:

    template <typename T>
    class foo_impl
    {
        T* data;
    public:
        void operator<<(const T& t);
    };
    
    template <typename T>
    struct foo : foo_impl<T>
    {
    };
    
    template <>
    struct foo<char> : foo_impl<char>
    {
        void operator<<(const char* str);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have method in a class that I need to make sure is only
I need to make a class that simplifies saving dynamic/input TextField text to an
I need to make a div layer so that when you click on it
I need to make a proxy script that can access a page hidden behind
I need to make a stored procedure or function that returns a set of
I'd like to use SQLiteCursor class to fetch data (not using Cursor class). This
I am trying to make a class with generic __init__ values, but have defaults
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have a generic dispatcher that takes in parameters and then make $.ajax() As
I have a class that performs some operations on a set of data. The

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.