Explicit Template Confusion
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Explicit Template Confusion

  1. #1
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Explicit Template Confusion

    Hi Gurus,

    Howdy, I've been gone a while.

    I would like to make specializations of a template subroutine within the specialization of a template struct.

    • I know that the standard does not allow explicit template specilizations within a non-namespace scope.
    • I also know that VC does allow it and does compiles it (non-standard).
    • I know that GCC complains (correctly) about "error: explicit specialization in non-namespace scope".
    • What I don't know is how to code it.

    I have the code below. It works for VC and is non-standard. It doesn't work with GCC because GCC is conformant and the code is not.

    Could anyone please tell me how to code it correctly?

    Thanks. Sincerely, Chris.

    Code:
    #include <iostream>
    
    namespace my_name
    {
      // Interface of outer
      template<const unsigned A, const unsigned B>
      struct outer { };
    
      // Specialization for A = 1, B = ?.
      template<const unsigned B>
      struct outer<1u, B>
      {
        template<const unsigned C> static void inner(void)
        {
          std::cout << "Not yet implemented" << std::endl;
        }
        // Specialization for A = 1, B = ?, C = 0
        template<> static void inner<0u>(void)
        {
          std::cout << (1u + B + 0u) << std::endl;
        }
        // Specialization for A = 1, B = ?, C = 1
        template<> static void inner<1u>(void)
        {
          std::cout << (1u + B + 1u) << std::endl;
        }
        // Specialization for A = 1, B = ?, C = 2
        template<> static void inner<2u>(void)
        {
          std::cout << (1u + B + 2u) << std::endl;
        }
      };
    }
    
    int main(void)
    {
      my_name::outer<1u, 2u>::inner<0u>(); // 3
      my_name::outer<1u, 2u>::inner<1u>(); // 4
      my_name::outer<1u, 2u>::inner<2u>(); // 5
      my_name::outer<1u, 2u>::inner<3u>(); // Not yet implemented
    }
    You're gonna go blind staring into that box all day.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,077

    Re: Explicit Template Confusion

    I'm quite sure you cannot specialize a member function template of a partially specialized class template unless outer<1u,B> is itself specialized ( for example, the code "template<> template<> void outer<1u, 0>::inner<0>(){}" at namespace scope placed after outer<1u,B> declaration defines a specialization of the member function template outer<1u,B>::inner() ).

    a solution could be

    Code:
    #include <iostream>
    
    namespace my_name
    {
      // Interface of outer
      template<const unsigned A, const unsigned B>
      struct outer { };
    
      template<const unsigned A,const unsigned B,const unsigned C>
      struct outer_impl{};
    
      // Specialization for A = 1, B = ?.
      template<const unsigned B>
      struct outer<1u, B>
      {
        template<const unsigned C> static void inner(void)
    	{
    		outer_impl<1u,B,C>::inner();
    	}
      };
    
      template<const unsigned B>
      struct outer_impl<1u,B,0u> { static inline void inner(){ /*whatever*/ } };
    
      template<const unsigned B>
      struct outer_impl<1u,B,1u> { static inline void inner(){ /*whatever*/ } };
    
      template<const unsigned B>
      struct outer_impl<1u,B,2u> { static inline void inner(){ /*whatever*/ } };
    
    }
    
    int main(void)
    {
      my_name::outer<1u, 2u>::inner<0u>(); // 3
      my_name::outer<1u, 2u>::inner<1u>(); // 4
      my_name::outer<1u, 2u>::inner<2u>(); // 5
      // my_name::outer<1u, 2u>::inner<3u>(); // Not yet implemented
    }
    the only difference being that this time my_name:uter<1u, 2u>::inner<3u>(); gives a compiler error instead ... ( I've not put a default inner implementation in the primary outer_impl definition in such a way to retain outer_impl as a generic "ternary" specialization model to be used by other outer<> partial specializations that might or might not define an inner() member ... )

  3. #3
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: Explicit Template Confusion

    Thanks superbonzo.

    I have used your suggestion, adapted to the application.
    The language issues are working out!
    The application still has a problem, but that is an unrelated issue.

    Always a pleasure at codeguru!

    Sincerely, Chris.
    You're gonna go blind staring into that box all day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center