Funktionen, die Einige return-Anweisungen enthalten, werden nicht als Inline expandiert



  • Hey,

    bei diesem Stückchen Code meldet der Borland C++ Compiler aus dem CBuilderX eine komische Warnung.

    #include <string>
    
    class Test
    {
    public:
    	std::string getName() const
    	{
    		return "Hallo";
    	}
    };
    
    int main()
    {
    }
    

    Warnung W8027 test.cpp 8: Funktionen, die Einige return-Anweisungen enthalten, werden nicht als Inline expandiert in Funktion Test::getName() const

    Wie kommt der darauf? Ich seh nur eine return-Anweisung. Und wenn ich die Funktion ohne Klasse drumherum schreibe, kommt keine Warnung. 😕



  • bdb schrieb:

    Warnung W8027 test.cpp 8: Funktionen, die Einige return-Anweisungen enthalten, werden nicht als Inline expandiert in Funktion Test::getName() const

    Schwachsinnige Fehlermeldung, offensichtlich 🙂

    Und wenn ich die Funktion ohne Klasse drumherum schreibe, kommt keine Warnung.

    Dann ist sie auch nicht inline.



  • Gemeint ist wohl der Konstruktoraufruf (std::string), der hier nicht expandiert wird.



  • Ist die Fehlermeldung vielleicht darauf hin zurück zu führen das Main nichts zurück gibt? Oder wird deshalb noch ne andere Fehlermeldung ausgegeben? Oder Gibt der Compiler bei erfolgreicher Beendung der Main-Funktion automatisch 0 zurück?



  • Nein. Das hat doch nichts mit inline Expandierungen zu tun.

    Wenn in der Funktion "main" kein Wert explizit zurückgegeben wird, wird 0 als Rückgabe angenommen.



  • Glaubst du das hier könnte vielleicht was damit zu tun haben?...

    class A {
    private:
    A* M_a;
    public:
    A(void) : M_a(this) { }
    A& get_a(void) const { return *M_a; }
    // ...
    };

    This example shows that even if we return a non-const reference to the very same object (M_a was initialized with this on creation), the function get_a is still constant: It doesn't change the object at all, it just returns the value of M_a (get_a() is an accessor).

    Trying to use const for class member functions for other reasons is wrong. Because the given class allows to change a constant object A we can conclude that the design of the class is wrong.

    The following example is correct and shows how to use const for member functions for overloading purposes.

    class B { };

    class A {
    private:
    B M_b;
    public:
    A(B const& b) : M_b(b) { }
    B& b(void) // Non-const member function
    { return M_b; } // because we return directly
    // a non-const reference to
    // to a member.
    B const& b(void) const // Overloaded for constant object.
    { return M_b; }
    };

    These two examples are very closely related. The difference is that in the second case we know first hand that the returned reference is a reference to a member of the class, while in the first example M_a can in principle point to any instance. Technically there is no difference between get_a and the first b(void), the only difference is the design of the two classes (bad design versus good design).

    Quelle: http://www.xs4all.nl/~carlo17/c++/const.qualifier.html

    Ich hab zwar nicht ganz verstanden wie das gemeint ist, aber ich kann es schon irgendwie mit unserem problem in Verbindung bringen.


Anmelden zum Antworten