vendredi 26 juin 2015

Code simpler than lambda for a call in constructor that uses an output parameter function for initializing a const member

In the header, I have

class CSomeClass
{
    const GUID m_guid;

public:
    CSomeClass();
///...
}

And in the source file

CSomeClass::CSomeClass()
    , m_guid(
        []() {
        GUID g;
        ::CoCreateGuid(&g);
        return g;
        }()
    )
{
}

Has you know Guids are not supposed to be mutable. Given the ::CocreateGuid() function returns what I want as an output parameter, I cannot use it directly with a simple call to it for initializing the m_guid member field, that is constant.

So, a consequence of its constness, is that it must be initialized before the opening bracket in initializer list, and therefore not be simply assigned with a call to ::CocreateGuid() in the constructor body.

Is there a simpler way to initialize it than this lambda expression?

Aucun commentaire:

Enregistrer un commentaire