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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:54:13+00:00 2026-06-03T00:54:13+00:00

Suppose we have a class: template <class Type> class A { public: void function1(float

  • 0

Suppose we have a class:

template <class Type>
class A
{
public:
    void function1(float a, Type b);
    void function1(float a, float b);
};

Now instantiate the class like this:

A<int> a;

It’s fine, this class will have 2 overloaded functions with these parameters: (float a, int b); (float a, float b);

But when you instantiate the class like this:

A<float> a;

You get compile error:

member function redeclared.

So, depending on the type of Type, I wan’t (or don’t want) the compiler to define a function, something like this:

template <class Type>
class A
{
public:
    void function1(float a, Type b);

    #if Type != float
    void function1(float a, float b);
    #endif
};

But, of course, the syntax above doesn’t work. Is it possible to perform such a task in C++? If possible, please provide an example.

  • 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-03T00:54:14+00:00Added an answer on June 3, 2026 at 12:54 am

    You could use template specialization:

    template <class Type>
    class A {
    public:
        void function1(float a, Type b) {
        }
        void function1(float a, float b) {
        }
    };
    
    template <>
    class A<float> {
    public:
        void function1(float a, float b) {
        }
    };
    
    // ...
    
    A<int> a_int;
    a_int.function1(23.4f, 1);
    a_int.function1(23.4f, 56.7f);
    
    A<float> a_float;
    a_float.function1(23.4f, 56.7f);
    

    — EDIT —

    If you have a large number of common functions, you could do something like this:

    class AImp {
    public:
        void function1(float a, float b) {
        }
        void function1(float a, double b) {
        }
        void function1(float a, const std::string& b) {
        }
        // Other functions...
    };
    
    template <class Type>
    class A : public AImp {
    public:
        void function1(float a, Type b) {
        }
        using AImp::function1;
    };
    
    template <>
    class A<float> : public AImp {
    };
    
    // ...
    
    A<int> a_int;
    a_int.function1(23.4f, 1);
    a_int.function1(23.4f, 56.7f);
    a_int.function1(23.4f, 56.7);
    a_int.function1(23.4f, "bar");
    
    A<float> a_float;
    a_float.function1(23.4f, 56.7f);
    a_float.function1(23.4f, 56.7);
    a_float.function1(23.4f, "bar");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have an autolocker class which looks something like this: template <T> class
suppose I have an object like this: class Spline { public: Spline(std::size_t const dim);
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have a class like class Empty{ Empty(int a){ cout << a; }
Suppose I have: template<typename T> class A { typedef T t_type; void done_work(); };
Suppose I have this class: template</*some other parameters here */class toggle> class Foo {

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.