0

Hi i am trying to understand how direct and copy initialization works and so have this example whose output i am unable to understand.

class DeletedCopy
{   public:
    
        DeletedCopy(const DeletedCopy&) = delete;
        DeletedCopy(DeletedCopy&) = delete;
        DeletedCopy()
        {
            std::cout<<"DeletedCopy Default constructor "<<std::endl;
        }
        DeletedCopy(DeletedCopy &&) 
        {
            std::cout<<"DeletedCopy move constructor"<<std::endl;
            
        }
};
class NAME
{
    private:
        std::string name;
        DeletedCopy temp;
    public:
        NAME() = default;
        NAME(const NAME& _name): name(_name.name)
        {
           std::cout<<"Copy constructor ran"<<std::endl; 
        }
        
        
        NAME(DeletedCopy d): name("v"),temp(std::move(d))
        {
            
          std::cout<<"explicit constructor NAME"<<std::endl;
        }
       
};
int main()
{
   cout << "Hello World" << endl; 

   
   NAME name3 = DeletedCopy();//this works and prints 3 lines on screen.
   NAME name4(DeletedCopy());//this doesn't print anything on console. Why doesn't this print anything. Is this statement UB
   
   return 0;
}

In this example when i used direct initialization, nothing is printed on the console. While if i use copy initialization then i get 3 lines printed on the console. Why isn't the direct initialization case working.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
Jason
  • 36,170
  • 5
  • 26
  • 60
  • 1
    `std::thread` is not copyable but it is movable. – Evg Aug 09 '21 at 07:40
  • If you know that, then why are you asking why it is taken by value instead of by reference? – Evg Aug 09 '21 at 07:42
  • Have you looked at the example 1 above. There the parameter is passed by value. That's why i am asking. – Jason Aug 09 '21 at 07:43
  • I have. After you tried to pass it by reference, what have you observed? Hint: `scoped_thread` takes ownership of a thread. Passing by value and moving from it is a canonical way of taking ownership. – Evg Aug 09 '21 at 07:49
  • `NAME name4(DeletedCopy());` — this is function declaration. A compiler should give you some warning about is. Live demo: https://godbolt.org/z/nxaTbfPrf. – Daniel Langr Aug 09 '21 at 07:50
  • @Evg Ok, i have updated the question. Thanks – Jason Aug 09 '21 at 07:50
  • @JasonLiam Your updated example is subject to [most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse#:~:text=The%20most%20vexing%20parse%20is,specification%20of%20a%20function's%20type.). `name4` is a function declaration. – super Aug 09 '21 at 07:51
  • Clang: `warning: parentheses were disambiguated as a function declaration [-Wvexing-parse] // note: add a pair of parentheses to declare a variable` – Evg Aug 09 '21 at 07:52
  • Oh yes, since the compiler that i was using didn't give any warning about this i forgot that this will be interpreted as a function declaration. Thanks. You can write it as an answer and i will accept that. Btw i already know about C++'s most vexing parse but forgot that it will be used in this case. – Jason Aug 09 '21 at 07:54
  • 1
    We'd better close it as a dupe. – Evg Aug 09 '21 at 07:55
  • @Evg Yup that's fine too. – Jason Aug 09 '21 at 07:55

0 Answers0