Trying to understand std :: forward, std :: move a little better

Now I am writing a base class template. Its parameters require two types of arguments. The idea of ​​a class is to take one type as quality const refand the other as ref. The functionality of the class is to convert the type Ainto the type Bwhere the created object will be B. I would like to have perfect-forwardingeither the move semanticsreal part of this class template.


Now here is my current class with basic types, but plan to deploy it to any 2 types using the variational construct.

#ifndef CONVERTER_H
#define CONVERTER_H

#include <utility>

template<class From, class To>
class Converter {
private:
    From in_;
    To   out_;

public:
    // Would like for From in to be a const (non modifiable) object
    // passed in by perfect forwarding or move semantics and for 
    // To out to be returned by reference with perfect forwarding 
    // or move semantics. Possible Constructor Declarations - Definitions

    // Using std::move
    Converter( From&& in, To&& out ) :
        in_{ std::move( in ) },
        out_{ std::move( out ) } 
    {
        // Code to convert in to out
    }

    // Or using std::forward
    Converter( From&& in, To&& out ) :
        in_{ std::forward<From>( in ) },
        out_{ std::forward<To>( out ) } {

        // Code to convert in to out.
     }        

    // Pseudo operator()... 
    To operator()() {
        return out_;
    }
};

#endif // !CONVERTER_H

std::move std::forward, . , , ()... :

int i = 10;
float f = 0;  
Converter<int, float> converter( i, f );

Visual Studio 2017 .

1>------ Build started: Project: ExceptionManager, Configuration: Debug Win32 ------
1>main.cpp
1>c:\users\skilz80\documents\visual studio 2017\projects\exceptionmanager\exceptionmanager\main.cpp(54): error C2664: 'Converter<unsigned int,float>::Converter(Converter<unsigned int,float> &&)': cannot convert argument 1 from 'unsigned int' to 'unsigned int &&'
1>c:\users\skilz80\documents\visual studio 2017\projects\exceptionmanager\exceptionmanager\main.cpp(54): note: You cannot bind an lvalue to an rvalue reference
1>Done building project "ExceptionManager.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

{ can't bind lvaule to rvalue ref}.


, :

int i = 10;
float f = 0;
Converter<int,float> converter( std::move( i ), std::move( f ) );

// Or
Converter<int,float> converter( std::forward<int>( i ), std::forward<float>( f ) );

, std::move(...) std::forward<T>(...).


, std::move(...) std::forward<T>(...) , , std::forward<T>(...) .


, , , std::move, , , , , std::forward<T> .


3 , .

  • std::move, std::forward , ; ? , , std::move() std::forward<T>() ?
  • A B ?
  • , , operator()() , ?

3 , , - std::any . , std::any , , ?


:

vector<int> vecFrom{1,2,3, ...};
set<int>    setTo;

Converter<vector<int>, set<int>> converter( vecFrom, setTo );

...

vector<int>     vecIntFrom{1,2,3, ...};
vector<string>  vecStringFrom{ "a", "b", "c", ... };
map<int,string> mapTo;
Converter<vector<int>, vector<string>, map<int,string> converter( vecIntFrom, vecStringFrom, mapTo );
+6
1

, , :

" std:: move std:: forward?"

std:: move lvalue rvalue, , lvalue .

std:: forward lvalue rvalue, , rvalue reference lvalue.

: , , std:: forward. lvalue rvalue, std:: move.

: http://thbecker.net/articles/rvalue_references/section_01.html

+2

All Articles