mercredi 1 mars 2017

How to initialize the std::optional of a user defined data type

I am trying to learn new features of c++11. Please find the below code:

class Test
{
public:
      Test(int x):y(x){}
      ~Test(){}
      int getx()
      {
          return x;
      }
private:
      int x;
};
struct Container
{
    std::optional<Test> test;
};
int main()
{
    struct Container obj;
    // here i want to initialize the member "test" of 
    // struct Container
    obj.test = make_optional<Test>(10); ----> is this correct ??
}

Can someone please let me know how to initialize a std::optional ? For example : if i declare like the below:

std::optional<Test> t

how can i initialize it ?

Aucun commentaire:

Enregistrer un commentaire