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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:54:32+00:00 2026-05-16T21:54:32+00:00

Hallo! I would like to specialise only one of two template types. E.g. template

  • 0

Hallo!

I would like to specialise only one of two template types. E.g. template <typename A, typename B> class X should have a special implementation for a single function X<float, sometype>::someFunc().

Sample code:

main.h:

#include <iostream>

template <typename F, typename I>
class B
{
public:
    void someFunc()
    {
        std::cout << "normal" << std::endl;
    };

    void someFuncNotSpecial()
    {
        std::cout << "normal" << std::endl;
    };
};

template <typename I>
void B<float, I>::someFunc();

main.cpp:

#include <iostream>
#include "main.h"

using namespace std;

template <typename I>
void B<float, I>::someFunc()
{
    cout << "special" << endl;
}

int main(int argc, char *argv[])
{
    B<int, int> b1;
    b1.someFunc();
    b1.someFuncNotSpecial();

    B<float, int> b2;
    b2.someFunc();
    b2.someFuncNotSpecial();
}

Compilation fails for class B. Is it true, that this is not possible in C++ in this way? What would be the best workaround?

[edit]

template <float, typename I>
void B<float, I>::someFunc();
leads to
main.h:26: error: ‘float’ is not a valid type for a template constant parameter

template <typename I>
void B<float, I>::someFunc();
leads to
main.h:27: error: invalid use of incomplete type ‘class B’

And I’m using gcc.

[edit]

I don’t want to specialise the whole class, as there are other functions that don’t have a specialisation.

  • 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-16T21:54:33+00:00Added an answer on May 16, 2026 at 9:54 pm

    You have to provide a partial specialization of the class template B:

    template <typename I>
    class B<float, I>
    {
    public:
        void someFunc();
    };
    
    template <typename I>
    void B<float, I>::someFunc()
    {
        ...
    }
    

    You can also just define someFunc inside the specialization.

    However, if you only want to specialize a function, and not a class do e. g.

    template <typename F, typename I>
    void someFunc(F f, I i) { someFuncImpl::act(f, i); }
    
    template <typename F, typename I>
    struct someFuncImpl { static void act(F f, I i) { ... } };
    
    // Partial specialization
    template <typename I>
    struct someFuncImpl<float, I> { static void act(float f, I i) { ... } };
    

    But you can’t specialize a function template without this trick.

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

Sidebar

Related Questions

Hallo I would like to sanitize input from Ruby, but simultaneously not have it
Hallo there I have and imageField as part of a gridview and would like
Hello I would like to display an other splashscreen after the default one, if
Hello I have a datatable that I would like to filter with a single
I have a string like this: String A: [ 12234_1_Hello'World_34433_22acb_4554344_accCC44 ] I would like
Hello I have 2 lists that contains the same objects. I would like to
I have a script that creates a table like this: class TableClass < Table
Hello I would like to have my app pick a randome XIB file to
Hallo, i would like to do something that i thought would be fairly simple:
I have two cells lets say: A1 and A2 The content of each one

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.