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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:42:29+00:00 2026-06-12T22:42:29+00:00

Suppose I have the following class: template <typename T> class MyClass { public: void

  • 0

Suppose I have the following class:

template <typename T>
class MyClass
{
public:
    void SetValue(const T &value) { m_value = value; }

private:
    T m_value;
};

How can I write a specialized version of the function, for T=float (or any other type)?

Note: A simple overload won’t suffice because I only want the function to be available for T=float (i.e. MyClass::SetValue(float) doesn’t make any sense in this instance).

  • 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-12T22:42:30+00:00Added an answer on June 12, 2026 at 10:42 pm
    template <typename T>
    class MyClass {
    private:
        T m_value;
    
    private:
        template<typename U>
        void doSetValue (const U & value) {
            std::cout << "template called" << std::endl;
            m_value = value;
        }
    
        void doSetValue (float value) {
            std::cout << "float called" << std::endl;
        }
    
    public:
        void SetValue(const T &value) { doSetValue (value); }
    
    };
    

    or (partial template specialization):

    template <typename T>
    class MyClass
    {
    private:
        T m_value;
    
    public:
        void SetValue(const T &value);
    
    };
    
    template<typename T>
    void MyClass<T>::SetValue (const T & value) {
        std::cout << "template called" << std::endl;
        m_value = value;
    }
    
    template<>
    void MyClass<float>::SetValue (const float & value) {
        std::cout << "float called" << std::endl;
    }
    

    or, if you want the functions to have different signatures

    template<typename T>
    class Helper {
    protected:
        T m_value;
        ~Helper () { }
    
    public:
        void SetValue(const T &value) {
            std::cout << "template called" << std::endl;
            m_value = value;
        }
    };
    
    template<>
    class Helper<float> {
    protected:
        float m_value;
        ~Helper () { }
    
    public:
        void SetValue(float value) {
            std::cout << "float called" << std::endl;
        }
    };
    
    template <typename T>
    class MyClass : public Helper<T> {
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following service object public class UserService { @Autowired private UserDao
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int
gcc 4.5.1, SuSE Linux i686 Suppose we have following code: template<typename realT> class B
Suppose I have a class template <typename T> class A { public: template <typename
Suppose I have got the following template template <typename T> class{ T t; };
Suppose we have the following template class template<typename T> class Wrap { /* ...
Suppose I have the following class template: template<typename T> struct Wrapper { T* t_;
Suppose you have the following class: class Test : ISerializable { public static Test
Suppose I have the following class: class Camera { public Camera( double exposure, double

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.