C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] Friend namespace
From: Ville Voutilainen (ville.voutilainen_at_[hidden])
Date: 2020-12-06 05:11:54


On Sun, 6 Dec 2020 at 03:57, chibane nadir via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hello Team,
> for the purpose of TDD, there is a problem with testing private data members and member functions. some call it black box testing and others do the nasty "#define private public" just to shortcut the visibility of private and protected members to test the classes.
> I suggest to add the feature of "friend namespace" like the following:
>
> namespace XYZ
> {
> // define here your functions and classes
> void test_object_private_data_member_D(object* self,...);
> }
>
> class object
> {
> // private data members, and member functions
> 'sometype' D;
> // public data members, and member functions
>
> // for the purpose of testing this class let's include the members of namespace XYZ as friends so we can access private and protected
> // members of the current class
> private:
> using friend namespace XYZ; // <---- (A)
> };

class object
{
// other stuff
#ifdef MY_TEST_DEFINE
    friend class my_object_test;
#endif
};

Trying to #define private to public is madness, but a
preprocessor-conditional friend is fine, and has been
used in various places for decades. We don't need a new language
facility for this.

Yes, you need to compile for testing separately, but that's often done
anyway, so that's not a showstopper.
You can also make the friending unconditional if that's your cup of
tea, then you don't need to compile
differently for testing and production.


STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups