Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Calling general template function from specialization

Calling general template function from specialization

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    How do I explicitly call the non-specialized sibling of a template function from within the specialized function?

    I have

    @template <class valueT>
    class CFuzzy
    {
    // Other methods...

    static bool isGreaterOrEqual(valueT value1,
                                 valueT value2);
    

    };

    template <>
    class CFuzzy<double>
    {
    // Other methods...

    static bool isGreaterOrEqual(double value1,
                                 double value2);
    

    };
    @

    CFuzzy has several methods that need to be specialized. However, isGreateOrEqual would not need to be specialized.
    Since I have to do it anyway, I am looking for a way to call my general template sibling, so as not to duplicate the code.

    Something like

    @bool CFuzzy<double>::isGreaterOrEqual(double value1,
    double value2)
    {
    CFuzzy<valueT>::isGreaterOrEqual(value1, value2); // This doesn't work
    }@

    Is this possible? If so, which syntax do I use?

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      There you should have CFuzzy<double>::... but that will recursively call the same function.

      Anyway i think you can't call that function from specialization, but an alternative is to specialize the functions not the class:
      @
      template <class valueT>
      class CFuzzy
      {
      public:
      // Other methods...
      static bool OtherMethod(valueT value);

      static bool isGreaterOrEqual(valueT value1,
                                   valueT value2);
      

      };

      //isGreaterOrEqual - never specialized
      template <class valueT>
      bool CFuzzy<valueT>::isGreaterOrEqual(valueT value1,
      valueT value2)
      {
      return value1 >= value2; //my dummy implementation
      }

      //OtherMethod - valueT template
      template <class valueT>
      bool CFuzzy<valueT>::OtherMethod(valueT value)
      {
      return value > 0; //my dummy implementation
      }

      //OtherMethod - template specialization for double
      template<>
      bool CFuzzy<double>::OtherMethod(double value)
      {
      return value > -1; //my dummy implementation
      }
      @

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        Thank you. I did even start out with functions, but found that putting functions that belong together into a class to be neater.

        I think, in this case, I'll go with the code duplication, properly test it and never change it again (or so I hope).

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved