Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 14–6–1 and injected-class-names
#1
Hi all,

Rule 14–6–1 says:

"In a class template with a dependent base, any name
that may be found in that dependent base shall be
referred to using a qualified-id or this->"

But then the example code contains this line:
Code:
typename B::TYPE t2 = 0; // Compliant - explicit use base TYPE
But I don't think that's correct, because although TYPE is qualified, B is not. I think this line should be moved into A::f1() and commented with "Non-compliant: B is unqualified". And add the line:
Code:
typename ::B::TYPE t2 = 0; // Compliant - explicit use of B and of TYPE.
...to f2().

Here's an example where it makes a difference:
Code:
template  
struct B  {
    static void j();
};

namespace N {
    template  
        struct B {
            static void j();
        };

}

template  
struct A : public N::B {
    static void f1 ( ) {
        A::template B::j(); // compliant
        A::B::j(); // compliant
    }
    static void f2 ( ) {
        B::j(); // non-compliant
    }
};

void h() {
    A::f1(); // calls ::N::B::j() twice
    A::f2(); // calls ::B::j() once
}
<r>James Widman<br/>
-- <br/>
Gimpel Software<br/>
<URL url="http://gimpel.com">http://gimpel.com</URL></r>
Reply
#2
For what it's worth, at least one implementation binds the call 'B::j()' to ::N::B::j() in the instantiation of ::A::f2(), but it should bind to ::B::j(). So it seems pretty clear that we need to regard the use of 'B' in f2() as non-compliant.

Also, since submitting the above post, I've found that some popular implementations don't correctly handle the uses of injected-class-names in the class template A above, so MISRA may want to say something about that. For example, MISRA may want to say that the 'safe' way to refer to a base class within a derived class involves a member typedef name; e.g.:

Code:
struct D : public N::B {
    typedef ::N::B B1; //compliant

    typedef N::B B2; // non-compliant
       // ('N' could be found in the base at instantiation time by a buggy compiler.)

    typedef B  B3; // non-compliant

    void f ( ) {
        B1::j(); // compliant
       // (unqualified lookup for 'B1' does not go up against a dependent base)
    }
};
<r>James Widman<br/>
-- <br/>
Gimpel Software<br/>
<URL url="http://gimpel.com">http://gimpel.com</URL></r>
Reply
#3
I agree. The example should probably be changed to:


Code:
typename A::B::TYPE t2 = 0; // Compliant - explicit use of B and of TYPE.

and for simplicity even:

Code:
typename A::B::TYPE t2 = 0; // Compliant - explicit use of B and of TYPE.

'A' and 'A' match the current specialization.

The rule text (or at least rationale) should be updated to highlight that this is required.


Regards,

Richard
<r>-- <br/>
Richard Corden<br/>
Programming Research Ltd.<br/>
<EMAIL email="[email protected]">[email protected]</EMAIL><br/>
+ 44 845 0048478</r>
Reply
#4
A future edition or Technical Corrigendum will consider changes along the lines suggested by Richard Corden's post
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)