MISRA Discussion Forums
Rule 14–6–1 and injected-class-names - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.14 Templates (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=142)
+---- Thread: Rule 14–6–1 and injected-class-names (/showthread.php?tid=544)



Rule 14–6–1 and injected-class-names - James Widman - 03-07-2008

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
}



Re: Rule 14–6–1 and injected-class-names - James Widman - 07-07-2008

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)
    }
};



Re: Rule 14–6–1 and injected-class-names - richard_corden - 16-07-2008

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


Re: Rule 14–6–1 and injected-class-names - misra cpp - 11-10-2016

A future edition or Technical Corrigendum will consider changes along the lines suggested by Richard Corden's post