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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:55:56+00:00 2026-06-09T12:55:56+00:00

I know from this thread – template class member function only specialization that if

  • 0

I know from this thread – template class member function only specialization
that if I specialize a class template, I need to specialize all the member functions.

So, my rationale was that I would ‘instantiate’ a template class. (Not sure if its the right way of saying it)

Then, since this instantiated class is a full-fledged class, I can specialize a particular method.

// I have a template class like this - 
template<class T, T invalidVal, int e> class A
{ 
     static inline bool dummy(T value) 
    {
        return 0;
    }
 }

 // Now I create classes from the above template class with 
 // T=void*, invalidVal=NULL & e=0 
 // And with T=void *, invalidVal=NULL & e=1 
 // And I typedef these classes to use them later
 typedef A<void *, (void *)NULL, 0> A_0;
 typedef A<void *, (void *)NULL, 1> A_1;

 // So now, what I was expecting was that A_0 & A_1 are classes and 
 // they can have their own specialized dummy() method
 // So I tried following - 
 inline bool A_0::dummy(void *value) 
 {
     return value != invalidVal;
 }

 inline bool A_1::dummy(void *value)
 {
     return value == invalidVal;
 }

The above code worked in a Windows environment. (Visual Studio 2008)

But this specialization does not work – using g++-4.7 – on Linux.
By reading through some other threads, I am also passing -std=c++11 to my g++ compilation command.

I get following 2 errors –

1. specializing member ‘A<void*, 0u, 0>::dummy’ requires ‘template<>’ syntax 
2. invalidVal was not declared in this scope

Now, error no. 1 goes away if I add a template<> before inline bool A_0::dummy(void *value) which makes me more worried, since I know the specialization did not happen as I wanted.

The error no. 2 doesn’t go away.

I am getting more and more confused about template class member function specialization.
What am I missing out here? Any reasons that g++ is complaining?
Any ideas?

Thanks!

  • 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-09T12:55:58+00:00Added an answer on June 9, 2026 at 12:55 pm

    I’m not sure I what you mean by “specialization did not happen as I wanted”. What exactly did you want?

    In your code you are trying to perform explicit specialization of template class member without specializing the class itself. All forms of explicit specialization in C++ require template<> syntax

    template<>
    inline bool A_0::dummy(void *value) 
    {
      // Whatever
    }
    
    template<>
    inline bool A_1::dummy(void *value)
    {
      // Whatever
    }
    

    That’s just the way it is in C++. Just because you “hid” the specialized class names behind typedef-names does not mean that your are excused from having to use the template<> syntax.

    Visual C++ apparently permits you to skip template <> part as a non-standard extension.

    Additionally, the specialized versions will know nothing about template parameter names (they don’t have any parameters anymore), meaning that name invalidVal is completely unknown to them. Either replace invalidVal with explicit NULL (as in explicit argument list), or make invalidVal value available through class as a constant static member

    template<class T, T invalidVal, int e> class A
    { 
      static const T iv; // declaration
      ...
    };
    
    template <class T, T invalidVal, int e>
    const T A<T, invalidVal, e>::iv = invalidVal; // definition
    
    template <> inline bool A_0::dummy(void *value) 
    {
       return value != iv;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Is anyone using NLTK with GAE? From this thread it appears that GAE does
I have learnt from this thread garbage collection with node.js that node.js uses a
If I have a thread that is started from the main application. In this
I know how to create a job dynamically from this create-sql-server-job-automatically The issue I
Does anybody know why the same code from this page http://emacsformacosx.com/ would not render
I would like to know how I can show the output from this piece
How to cut part from this string... abb.c.d+de.ee+f.xxx+qaa.+.,,s, ... where i know position by
How would I remove a view from this dialog? I know I can remove
I'm having trouble getting any information to display from this query. Anyone know where

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.