Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » moveable with assert question
moveable with assert question [message #52369] Sun, 15 September 2019 17:30 Go to next message
mtdew3q is currently offline  mtdew3q
Messages: 171
Registered: July 2010
Location: Upstate, NY (near Canada)
Experienced Member

Hi all-

I thought this code below would kick out an error when compiled that it is a moveable violation.

Can you please give me a tip on how to check for moveable with the code below?

thnx. roboloki

#include <Core/Core.h>

using namespace Upp;

class Foo: Moveable<Foo> {
    int a;
    Foo *foo;
    int *ptr;
public:
    void Set(Foo * f) {
        foo = f;
    }
  
void Bad1() {
        foo = this;
    // member variable foo exists outside method
    // -> makes Foo non-moveable
    }       
    ~Foo() {
     
    }
};

template <typename T> class Metallica
{
   T * t;
   public:
   
   Metallica (){};
   
   void Set(T * t1) {
        t = t1;
    
    
   }
   
    ~Metallica() {
    AssertMoveable<T>();  
    }
};

CONSOLE_APP_MAIN
{
	
	Metallica<Foo> lu;
	
}
Re: moveable with assert question [message #52372 is a reply to message #52369] Sun, 15 September 2019 19:38 Go to previous messageGo to next message
mtdew3q is currently offline  mtdew3q
Messages: 171
Registered: July 2010
Location: Upstate, NY (near Canada)
Experienced Member

Hi all-

I am trying to learn the core and requirements for moveable.
Do classes still have to be moveable for pick?

I understand that pick might be a synonym for std::move.

Here is another code snippet. I am not sure why it won't work.
Please see the small class in my other post. The vector code fails
when adding a foo object. // v1.Add(f);

Please show me a trick. thnx. roboloki

	Foo f, f2;
	
	f.Set(&f2);
   
    //Foo && r2 = std::move(f) ;
    
    // int b = r2.Get();
    
    //  Cout() << b;
    
    Vector<Foo> v1;
    
    v1.Add(f);
    
    Vector<int> n;
    n.Add(3);
  
Re: moveable with assert question [message #52373 is a reply to message #52372] Sun, 15 September 2019 20:51 Go to previous messageGo to next message
mtdew3q is currently offline  mtdew3q
Messages: 171
Registered: July 2010
Location: Upstate, NY (near Canada)
Experienced Member

Hi Mirek and friends:

I don't really need those constructors like assignment and copy unless I have a pointer as a class member. I think I probably could implement a move assignment function and copy assignment function for move etc. if I needed to. I took the pointers out and then it compiled fine.

I will go put a pointer as a member and make a move copy constructor and check it out.

thanks,
roboloki



Re: moveable with assert question [message #52374 is a reply to message #52373] Sun, 15 September 2019 21:26 Go to previous messageGo to next message
mtdew3q is currently offline  mtdew3q
Messages: 171
Registered: July 2010
Location: Upstate, NY (near Canada)
Experienced Member

Hi all-

It is still not working. I will list the last attempt here. Not sure what I am doing wrong!

class Foo:Moveable<Foo>  {
    
    char * buffer;
public:
    
    Foo(){
    buffer = NULL;
    }
    
    Foo(const char * c) {
        if( c!=NULL)
        {
            buffer= new char [strlen(c) + 1];
            strcpy(buffer, c);
        } else
             buffer = NULL;
    }
    
  Foo(Foo&& foosrc) {
      if(foosrc.buffer !=NULL) {
          buffer=foosrc.buffer;
         foosrc.buffer = NULL;
      }
}



CONSOLE_APP_MAIN
{
    Foo f("foo");
    Foo && r2 = std::move(f) ;
    
    Vector<Foo> v1;
     
     v1.Add(r2);
  	
}


If I am making a glaring error that you can see please help me with the trick!

thanks roboloki
Re: moveable with assert question [message #52375 is a reply to message #52374] Mon, 16 September 2019 04:08 Go to previous messageGo to next message
mtdew3q is currently offline  mtdew3q
Messages: 171
Registered: July 2010
Location: Upstate, NY (near Canada)
Experienced Member

Hi Mirek and friends:

I solved the problem. It kept telling me something about deleted. Then I switched views from the error view in U++ to the view that gives more detail.

I searched on the Internet and I found this link https:// stackoverflow.com/questions/43954808/why-is-the-move-constru ctor-defined-and-the-assignment-operator-implicitly-delet

I checked my gcc version and then decided to tack on default to one of my methods that didn't have it.

Here is the updated code that works.

class Foo: Moveable<Foo>  {
    
    char * buffer;
    
public:
	Foo()=default;
    Foo(const char * c);
    Foo( const Foo&) =default;
   
    Foo(Foo&&) = default;
    Foo& operator=(Foo&&) = default;
    String Get(){
        return buffer;
    }
    ~Foo() {
     AssertMoveable<Foo>();   
     delete buffer;
    }
};

Foo::Foo(const char * c) {
        if( c!=NULL)
        {
            buffer= new char [strlen(c) + 1];
            strcpy(buffer, c);
        } else
             buffer = NULL;
    }
   
  


CONSOLE_APP_MAIN
{
    Foo f("foo");
	Foo && r2 = std::move(f); 
    Foo &  j = r2 ;
    
    Vector<Foo> a, b;
    b.Add(r2);
    
    Foo & myval = b.At(0);
    Cout() << myval.Get();
  
  	
}


thnx : roboloki
Re: moveable with assert question [message #52376 is a reply to message #52375] Mon, 16 September 2019 05:29 Go to previous messageGo to next message
mtdew3q is currently offline  mtdew3q
Messages: 171
Registered: July 2010
Location: Upstate, NY (near Canada)
Experienced Member

Hi all-

I think the references get invalidated in this case. I will work on my understanding of that next. I am thinking either somehow I can use a smart pointer U++ style or C++ kind | pick a different container than vector.

I am headed back to a vicious work cycle thought (Mon. thru Fri.)

Hope everyone has a cool week Smile

roboloki
Re: moveable with assert question [message #52378 is a reply to message #52376] Mon, 16 September 2019 10:10 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12565
Registered: November 2005
Ultimate Member
I think the one thing that you misunderstood is that it is up to programmer to decide what is Moveable and what is not. Unfortunately, this too complex for any compiler check, so programmers is required to mark Moveable types (by deriving from Moveable<T>).

Mirek
Re: moveable with assert question [message #52379 is a reply to message #52369] Mon, 16 September 2019 13:37 Go to previous message
mtdew3q is currently offline  mtdew3q
Messages: 171
Registered: July 2010
Location: Upstate, NY (near Canada)
Experienced Member

Hi mirek.

Oh...

No worries. You have good tips on deciding in the documentation.

Thnx again,
Jim
Previous Topic: SetVppLogNoDeleteOnStartup() declaration is missing
Next Topic: FileSize
Goto Forum:
  


Current Time: Thu Jul 02 11:18:24 CEST 2020

Total time taken to generate the page: 0.01685 seconds