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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:19:58+00:00 2026-06-12T19:19:58+00:00

I can use partial template specialization inside class declaration template<class T1, class T2> struct

  • 0

I can use partial template specialization inside class declaration

template<class T1, class T2>
struct A
{
    void foo() { cout << "general"; }
};

template<class T1>
struct A<T1, int>
{
    void foo() { cout << "partial specialization"; }
};

But when I’m trying to do it outside class declaration

template<class T1, class T2>
struct A
{
    void foo();
};


template<class T1, class T2>
void A<T1, T2>::foo() { cout << "general"; }

template<class T1>
void A<T1, int>::foo() { cout << "partial specialization"; }

I get the following error:

invalid use of incomplete type «struct A < T1, int >»

It’s not a problem to use the first approach when you want to redefine all members, but what if you want redefine only one method without code duplication for all others?

So, is it possible to use partial template specialization outside class definition?

  • 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-12T19:20:00+00:00Added an answer on June 12, 2026 at 7:20 pm

    It’s not a problem to use the first approach when you want to redefine all members, but what if you want redefine only one method without code duplication for all others?

    This is where traits technique can be used. See http://www.boost.org/community/generic_programming.html#traits

    Some usage:

    template <class T1, class T2>
    struct ATraits {
       static void foo() {}
    };
    
    template <class T1>
    struct ATraits<T1,int> {
       static void foo() {}
    };
    
    template <class T1, class T2>
    struct A {
       void foo() { ATraits<T1,T2>::foo(); }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use partial specialization for a template class so that all children
I'm trying to use partial template specialization on a (non-member) function, and I'm tripping
Possible Duplicate: “invalid use of incomplete type” error with partial template specialization Why is
I've been reading over the internet about a partial method that I can use
I can use .button() to create beautiful submit buttons, but the rest of the
I have a partial that I'd like to use in a layout but when
for example, I hava a partial template named _header.php, I want to use it
I am studying C++ concepts - Template specialization and Partial template specialization. Have a
I am trying to use a partial view editor template in my application using
I can use the following to get the contents of a folder with no

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.