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:
Converter( From&& in, To&& out ) :
in_{ std::move( in ) },
out_{ std::move( out ) }
{
}
Converter( From&& in, To&& out ) :
in_{ std::forward<From>( in ) },
out_{ std::forward<To>( out ) } {
}
To operator()() {
return out_;
}
};
#endif
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 ) );
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 );