0

As the subject, the related code is listed below.You could check it on https://godbolt.org/z/mAbmwJ.

I completely understand the differences between memcpy(3) and memmove(3), but I don't understand the reasons lie behind it.The code is quoted from an wellknown open source project, I don't doublt the correctness of the code and the developers must have some reasons to do so. I would be grateful to have some help with this question.

#include<string.h>
#include<iostream>

#define ENTITYID_UNKNOWN 0x00000000
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER  0x000003c2
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER  0x000003c1

struct EntityId_t
{
    static constexpr unsigned int size = 4;
    char value[size];
    //! Default constructor. Uknown entity.
    EntityId_t(){
        *this = ENTITYID_UNKNOWN;
    }

    EntityId_t(int id)
    {
        int* aux = (int*)(value);
        *aux = id;
         std::cout << "EntityId_t(int id) constructor" << std::endl;
    }

    /*!
     * @brief Copy constructor
     */
    EntityId_t(
            const EntityId_t& id)
    {
        memcpy(value, id.value, size);
        std::cout << "copy constructor" << std::endl;
    }

    EntityId_t& operator =(
            const EntityId_t& id)
    {
        memcpy(value, id.value, size);
        std::cout << "copy operator() constructor" << std::endl;
        return *this;
    }

    /*!
     * @brief Move constructor
     */
    EntityId_t(
            EntityId_t&& id)
    {
        memmove(value, id.value, size);
        std::cout << "move constructor" << std::endl;
    }

    EntityId_t& operator =(
            EntityId_t&& id)
    {
        memmove(value, id.value, size);
        std::cout << "move operator(EntityId_t&&)" << std::endl;
        return *this;
    }
};



int main()
{
    EntityId_t c_SEDPPubWriter = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER;
    std::cout << "==============================" << std::endl;

    EntityId_t c_SEDSubscribe;
    c_SEDSubscribe = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER;
}

Output:

EntityId_t(int id) constructor
==============================
EntityId_t(int id) constructor
move operator(EntityId_t&&)
EntityId_t(int id) constructor
move operator(EntityId_t&&)
John
  • 1,029
  • 2
  • 14
  • When you see a line like: `*this = ENTITYID_UNKNOWN;` (`ENTITYID_UNKNOWN` is `0x00000000`) you know strange things are ahead. – Richard Critten Jun 05 '20 at 15:46
  • On the contrary,i think the outputs after "===" are right.I can completly understand it.But i can't undertand the outputs before "===".I would reedit the code to avoid the misunderstanding.I think it should call the user defined constructor EntityId_t(int id) **and movement assignment operator,** – John Jun 05 '20 at 16:09
  • Note the `=` in the first statement of `main` is not an assignment operator. It just introduces the variable's initializer. – aschepler Jun 06 '20 at 00:27
  • Could you explain that in more detail?I think `ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER` calls the user defined constructor `EntityId_t(int id)` to produce a temporary object.Since it's a rvalue(temporary object), complier then calls the `movement assigment operation`.Where am i wrong? – John Jun 06 '20 at 00:49
  • `EntityId_t c_SEDPPubWriter = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER;` calls `EntityId_t(int id)` constructor. Assignment operator is not involved at all. To the first approximation, there's no difference between `SomeClass var = init;` and `SomeClass var(init);` - it's a different syntax for initialization (again, there is no assignment either way). – Igor Tandetnik Jun 06 '20 at 03:57
  • @IgorTandetnik Why `EntityId_t c_SEDPPubWriter = EntityId_t(ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER);` does not call `movement assignment operator`, either?It's so strange. – John Jun 06 '20 at 04:26
  • No. At best, it may call a move constructor; but even that is usually (since C++17, always) [elided](https://en.cppreference.com/w/cpp/language/copy_elision) – Igor Tandetnik Jun 06 '20 at 04:33
  • @Igor Tandetnik I’m sorry, I missed that. I am not native speaker. Could you rephrase that for me in simpler words? I didn’t quite catch your meaning. – John Jun 06 '20 at 05:30

0 Answers0