vendredi 18 septembre 2020

Is this the right way to use std::move?

I am trying to save a copy of char[10] in the below code snippet, just would like to know if this is the right way to use std::move and this will in fact save a copy of char[10] as Struct2 object is being constructed.

#include <string>
#include <iostream>
#include <cstring>
#include <memory>
using namespace std;


struct Struct1 {
    int a;
    string b;
    char c[10];
};

struct Struct2 {
    const Struct1 struct1;
    int d;
    string e;
};

int main() {
    int a = 10;
    auto b = "b";
    char c[10] = "12345";
    Struct1 struct1{a, b};
    strcpy(struct1.c, c);
    Struct2 str2{std::move(struct1), 10, "e"}; //<-- no copy of struct1.c, right?
    cout << str2.struct1.c << endl;
}

Also is there a better syntax in construct Struct2, if I dont want to copy char[10] twice(Struct1 will not be used other than as a field of Struct2)?

Aucun commentaire:

Enregistrer un commentaire