c++ref()関数
1625 ワード
std::refを使用すると、テンプレートのパラメータを転送するときに参照を転送できます.そうしないと、転送できません.
ref()メソッドの戻り値はreference_wrapperタイプ、このクラスのソースコードは、ポインタを維持し、オペレータを再ロードすることを意味します.
#include
template
void com(T arg)
{
std::cout <
ref()メソッドの戻り値はreference_wrapperタイプ、このクラスのソースコードは、ポインタを維持し、オペレータを再ロードすることを意味します.
template
class reference_wrapper
: public _Refwrap_impl<_ty>
{ // stand-in for an assignable reference
public:
typedef reference_wrapper<_ty> _Myt;
typedef _Refwrap_impl<_ty> _Mybase;
typedef _Ty type;
reference_wrapper(_Ty& _Val) _NOEXCEPT
: _Mybase(_Val)
{ // construct
}
reference_wrapper(const _Myt& _Right) _NOEXCEPT
: _Mybase(_Right.get())
{ // construct by copying _Right
}
_Myt& operator=(const _Myt& _Right) _NOEXCEPT
{ // assign _Right
this->_Reset(_Right.get());
return (*this);
}
operator _Ty&() const _NOEXCEPT
{ // return reference
return (this->_Get());
}
_Ty& get() const _NOEXCEPT
{ // return reference
return (this->_Get());
}
reference_wrapper(_Ty&&) = delete;
};
#include
template
void com(T arg)
{
arg++;
}
template
struct MyStruct
{
T &t;
MyStruct(T &t):t(t){
}
T& get(){
return &t;
}
void operator++(int) //
{
this->t++;
}
};
using namespace std;
int main(){
int count = 10;
int & rcount = count;
MyStruct m(rcount);
com(m);
std::cout << rcount << endl; //11
getchar();
return 0;
}