CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jan 2011
    Posts
    101

    Perfect forwarding problem

    Code:
    // base class
    class resource
    {
    protected:
    	std::string name;
    public:
    	template< typename TStr >
    	resource(TStr&& n) : name(std::forward<TStr>(n)) {}
    
    	resource(resource&& o) : name(std::move(o.name)) 
    	{ 
    	}
            ...
    };
    
    // child
    class image : public resource
    {
    private:
    	int* img_data;
    public:
    	template< typename TStr >
    	image(TStr&& n) : resource(std::forward<TStr>(n)), img_data(new int[500]) {}
    
    	image(image&& o) : resource(std::move(o)), img_data(o.img_data)
    	{
    		o.img_data = nullptr;
    	}
            ...
    };
    When the call is made to image move-copy-ctor
    Code:
    resource(std::move(o))
    want bind to resource move-copy-ctor but instead to ctor that excepts string.
    How to resolve this?

    Thanks for your time.

    EDIT: SOLVED!
    Code:
    //template< typename TStr >
    resource(std::string&& n) : name(std::forward<std::string>(n)) {}
    I should use perfect forwarding only for functions/methods with multiple parameters.
    Last edited by borko1980; February 20th, 2012 at 10:54 AM. Reason: solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured