this->a oder a oder template<int a>



  • Hoi,

    Ich hatte so ein änliches Thema schonmal gepostet, aber jetzt ist mir noch eine neue Frage gekommen.
    Folgende Situation:

    template<typenmae T,int Size>
    class Foo
    {
    public:
        int Size;
    
        void func(int Size);
    };
    
    template<typenmae T,int Size>
    void Foo<T,Size>::func(int Size)
    {
        this->Size=Size; // Welches Size wird aufgerufen?
    }
    


  • Wer weiß da schon. Tu was für gescheite Namensgebung, dann wirds solche Probleme auch niemals geben...



  • Ich würd einfach mal sagen, dass das nen Fehler beim compilieren gibt.

    gcc4.1.1 schrieb:

    test.cpp:5: error: declaration of 'int Foo<T, Size>::Size'
    test.cpp:1: error: shadows template parm 'int Size'

    ansonsten hat david_pb recht 😉


  • Mod

    der Comeau C++ online compiler mag das auch nicht. vc++ 8.0 läßt es zu. imo könnte der standard an dieser stelle deutlicher werden:

    ISO/IEC 14882:2003 schrieb:

    14.1 Template parameters /13

    The scope of a template-parameter extends from its point of declaration until the end of its template. In particular, a template-parameter can be used in the declaration of subsequent template-parameters and their default arguments. [Example:
    template<class T, T* p, class U = T> class X { /* ... / };
    template<class T> void f(T
    p = new T);
    —end example]

    entscheidend daran ist die wortwahl: "scope" und nicht "potential scope" oder "deklarative region". folglich darf dieser bezeichner auch nicht in irgendwelchen verschachtelten scopes redeklariert werden.
    beispiel:

    template<typename T,int Size>
    class Foo
    {
    public:
        int S;
    
        void func(int S);
    };
    
    template<typename T,int S>
    void Foo<T,S>::func(int Size)
    {
        {
            this->S=Size;
            int S;    // fehler
            int Size; // ok
        }
    }
    


  • Hi!

    camper schrieb:

    void Foo<T,S>::func(int Size)
    {
        {
            this->S=Size;
            int S;    // fehler
            int Size; // ok
        }
    }
    

    Sowas is zwar erlaubt, trägt aber dennoch nich gerade zu übersichtlichem Code bei...


Anmelden zum Antworten