Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-18412

"Assign to local variable" deduces wrong type for types of templated class

XMLWordPrintable

    • 15665680df (qt-creator/qt-creator/master)

      Qt Creators "Assign to local variable" refactoring action is not able to deduce the correct return types for templated classes in the following situations:

      • When a global typedef is used for the templated class (`IntClass` in my example)
      • When a special type is defined in the templated class (`TemplateClass::A` in my example)

      My test situation is a templated class with a globally defined typedef for integer types:

      template<typename Type>
      class TemplateClass
      {
      public:
        typedef float A;
        A GetA ();
      
        Type GetType ();
      };
      typedef TemplateClass<int> IntClass;
      

       

      1. The first (worst) test case is:

      void test ()
      {
        IntClass i;
      
        // Using "Assign to local variable"
        TemplateClass::A a = i.GetA (); // WRONG
        Type type1 = i.GetType(); // WRONG
      }
      

      The desired outcome is:

        IntClass::A a = i.GetA ();
        int type = i.GetType();
      

      Possible alternatives are:

        TemplateClass<int>::A a3 = i.GetA();
        float a4 = i.GetA();
      

       

      2. A second test case using a locally defined typedef for float types is:

      void testLocalTypedef ()
      {
        // Using "Assign to local variable" with locally defined typedef
        typedef TemplateClass<float> FloatClass;
        FloatClass f;
        TemplateClass::A a = f.GetA(); // WRONG
        float type = f.GetType(); // OK
      }
      

       

      3. A third test case without using a typedef is:

      void testWithoutTypedef()
      {
        // Using "Assign to local variable" without typedef
        TemplateClass<double> d;
        TemplateClass::A a = d.GetA(); // WRONG
        double type = d.GetType(); // OK
      }
      

        For Gerrit Dashboard: QTCREATORBUG-18412
        # Subject Branch Project Status CR V

            kandeler Christian Kandeler
            m3197d - -
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes