mercredi 18 septembre 2019

Unable to use brace enclosed initializer-list while inheriting from friend class

I am trying to use an initializer list for a data structure which inherits from its parents friend class's subclass. Below I compiled an example which demonstrates the problem(in c++11).

#include <iostream>
#include <vector>

class i_gossip;

class i_have_secrets{
    friend class i_gossip;
public:
    i_have_secrets();
private:
    struct secret_t{
        int secret_number;
        std::vector<int> secret_vector;
    }i_am_secret;
};

class i_gossip{
public:
    struct i_am_secret : public i_have_secrets::secret_t { };
};

i_have_secrets::i_have_secrets(){
    i_am_secret = {0, {0,1,2}}; // Totally fine
}

int main(int argc,char** args){
    i_gossip::i_am_secret secret = {0, {0,1,2}}; // Compile error
    return 0;
}


The declaration is fine, but the initialization isn't, it gives the error could not convert {...} from '<brace-enclosed initializer list>' to i_gossip::i_am_secret secret. It is possible to compile the program by adressing and setting each induvidual members of the struct by so:

    i_gossip::i_am_secret secret;
    secret.secret_number = 0;
    secret.secret_vector = {0,1,2};

If the members are available to use, why does an initialization-list fails with a compile error?

Aucun commentaire:

Enregistrer un commentaire