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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:05:25+00:00 2026-06-05T04:05:25+00:00

When I try to specialize a public member function template within the class definition/declaration:

  • 0

When I try to specialize a public member function template within the class definition/declaration:

#include <iostream>

class surfaceMesh 
{
    public:
        // Uncomment for Version 0 and 1
        class AREA_AVERAGE {};
        class ANGLE_AVERAGE {};  

        template<class Average>
        void vertexNormals() {}

        // Uncomment: Version 0
        //template<>
        //void vertexNormals<AREA_AVERAGE> ()
        //{
            //std::cout << "AREA_AVERAGE" << std::endl;
        //}

        //template<>
        //void vertexNormals<ANGLE_AVERAGE> ()
        //{
            //std::cout << "ANGLE_AVERAGE" << std::endl;
        //}


};

// Uncommend for version 1 
template<>
void surfaceMesh::vertexNormals<surfaceMesh::AREA_AVERAGE> ()
{
    std::cout << "AREA_AVERAGE" << std::endl;
};

template<>
void surfaceMesh::vertexNormals<surfaceMesh::ANGLE_AVERAGE> ()
{
    std::cout << "ANGLE_AVERAGE" << std::endl;
};


int main()
{
    surfaceMesh m;

    m.vertexNormals<surfaceMesh::AREA_AVERAGE>();
    m.vertexNormals<surfaceMesh::ANGLE_AVERAGE>();

    return 0;
}

For Version 0, the error is:

main.cpp:19: error: template-id ‘vertexNormals<mesh::AREA_AVERAGE>’ in declaration of primary template
main.cpp:24: error: explicit specialization in non-namespace scope ‘class mesh’
main.cpp:25: error: template-id ‘vertexNormals<mesh::ANGLE_AVERAGE>’ in declaration of primary template
main.cpp:25: error: ‘void mesh::vertexNormals()’ cannot be overloaded
main.cpp:19: error: with ‘void mesh::vertexNormals()’

Version 1 compiles and runs. Of course, usually I am separating the class declaration and definition, but I would really like to know why this happens.

Also, is this a good way to specialize an interface? The other option would be to overload the function vertexNormals to take an object of AREA_AVERAGE or ANGLE_AVERAGE, but this is just a type telling me which kind of function I will be using, it is not supposed to be instantiated, so using templates ‘feels’ like a right choice.

  • 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-05T04:05:27+00:00Added an answer on June 5, 2026 at 4:05 am

    Why is it not allowed to specialize a member function template within a class?

    Because that is the rule laid down by the C++ Standard.

    As for what you want, a better approach is to use function overload rather than function specialization as:

    class surfaceMesh 
    {
        public:
            // Uncomment for Version 0 and 1
            class AREA_AVERAGE {};
            class ANGLE_AVERAGE {};  
    
            template<class Average>
            void vertexNormals() 
            {
               //invoke the function overload
               vertexNormals(static_cast<Average*>(0));
            }
    private:
            //make the overloads private, so client will not call them!
            void vertexNormals(AREA_AVERAGE *)
            {
               std::cout << "AREA_AVERAGE" << std::endl;
            }
            void vertexNormals(ANGLE_AVERAGE*)
            {
               std::cout << "ANGLE_AVERAGE " << std::endl;
            }
    };
    

    The type of the expression static_cast<Average*>(0) helps the compiler to choose the correct overload.

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

Sidebar

Related Questions

I try to have a partially specialized template class inheriting from another template class.
One of my classes declares a templated function: template<class A, class B> A do_something(const
I have a syntax similar to this: template <class P> class Foo : public
When I try to serialize a populated instance of type List<C>() where: public class
I have the following class: #pragma once #include <string> #include <iostream> class testclass {
I try to serialize a class, defined as following: [DataContract] public class Human {
The following code compiles properly. #include <string> template <typename T, typename U> class Container
I need to serialize a Lucene.net Document instance. When I try the following public
When I try to serialize class with protected members, I get the following errors:
Here's my code: [Serializable()] public class Project { private List<string> _Kinds = new List<string>();

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.